Page 1 of 1

[Solved] Search .txt file for random strings

Posted: Thu 17 Feb 2011, 13:54
by stu90
Hello,
I am wondering if anyone could possible help me.

If i have a .txt file containing:
some|random|text|here|

The only constant in the file is the | characters the actual four words between could be different each time - how would i extract individually what ever is in-between those | charters?
example:

random|random|random|random|
There is a random third word there, i don't know what is is so how to extract just that word?

what would be the best way to go about this?

Would it be possible / easier to somehow reformat the file into something more usable like?

some
random
text
here


thanks.

Posted: Thu 17 Feb 2011, 14:17
by jamesbond

Code: Select all

cat file.txt |  awk 'BEGIN { FS="|" } { print $1, $2, $3, $4 }' 
Explanation
FS="|" set the field separator to |
$1 = first word
$2 = second word
etc.

Re: Search .txt file for random string between two fixed str

Posted: Thu 17 Feb 2011, 15:02
by ken geometrics
stu90 wrote:Hello,
I am wondering if anyone could possible help me.

If i have a .txt file containing:
some|random|text|here|
# echo "hi|there|how|are|you">temp.txt
# echo "hi|there|how|are?you">>temp.txt
# grep -r "[a-z]*|[a-z]*|[a-z]*|[a-z]*|[a-z]*" temp.txt
hi|there|how|are|you
#
# grep -r "[a-z]*|[a-z]*|[a-z]*|[a-z]*|[a-z]*" temp.txt | cut -d"|" -f2
there
#

Posted: Thu 17 Feb 2011, 15:31
by stu90
Excellent - thanks guys! 8)

Posted: Thu 17 Feb 2011, 15:34
by big_bass
Hey stu90
random|random|random|random|
There is a random third word there, i don't know what is is so how to extract just that word?

what would be the best way to go about this?
the best way is what is easy fast and you understand how to make edits to it
so here is another option

Joe


lets say this is the content of somefile.txt
cat|dog|fish|elephant

Code: Select all

cat somefile.txt | grep "|" | tr '|' ' ' | awk '{ print $3 }'

this is more code but is very flexible grep looks for "|"

tr changes the pipe '|' for a space ' '

awk now pulls out the third string only

Posted: Thu 17 Feb 2011, 16:59
by stu90
big_bass wrote:Hey stu90
random|random|random|random|
There is a random third word there, i don't know what is is so how to extract just that word?

what would be the best way to go about this?
the best way is what is easy fast and you understand how to make edits to it
so here is another option

Joe


lets say this is the content of somefile.txt
cat|dog|fish|elephant

Code: Select all

cat somefile.txt | grep "|" | tr '|' ' ' | awk '{ print $3 }'

this is more code but is very flexible grep looks for "|"

tr changes the pipe '|' for a space ' '

awk now pulls out the third string only
Hi,
thanks for reply Big Bass - even after several years of using Linux now i am still overwhelmed by all choice if offers - i only wish my mind was like a sponge so i could absorb it all, instead i seem to have been lumbered with only a short term memory information goes in one ear and out t'other just as fast. :)

Getting back on track - I have another question if i may.

Now i can return the individual entries is it possible to run a command on that entry?

Example - entry three:
cat|dog|/root/sunnyday.jpg|elephant|

I get the path/name /root/sunnyday.jpg in terminal but how to pass that path/name to say image viewer /usr/bin/viewnior so i can view the image?

thanks.

Posted: Thu 17 Feb 2011, 18:18
by big_bass
using an icon I have
cat|dog|/root/puppy-reference/pixmaps/gftp.png|elephant

P.S I have mtpaint as an example as the command

Joe

Code: Select all

mtpaint `cat somefile.txt | grep "|" | tr '|' ' ' | awk '{ print $3 }'`
there are very small tick marks enclosing everything

Posted: Thu 17 Feb 2011, 18:32
by stu90
big_bass wrote:using an icon I have
cat|dog|/root/puppy-reference/pixmaps/gftp.png|elephant

P.S I have mtpaint as an example as the command

Joe

Code: Select all

mtpaint `cat somefile.txt | grep "|" | tr '|' ' ' | awk '{ print $3 }'`
there are very small tick marks enclosing everything
Cracking, thanks Joe. 8)

I think this is another way as well

Code: Select all

name1=$(cat /tmp/test | grep "|" | tr '|' ' ' | awk '{ print $1 }') && /usr/bin/viewnior $name1
cheers

Posted: Thu 17 Feb 2011, 19:41
by technosaurus
or you can use

Code: Select all

cut -s -d "|" -f 3 $file
-d is the delimiter
-s means only output lines containing the delimiter
-f is the field number
$file is the file name
(this method only uses one binary and no pipes, so it should also be pretty fast ... if it matters ... even faster if you use a recent busybox with prefer applets and #!/bin/ash as your shell)

Posted: Fri 18 Feb 2011, 03:22
by stu90
Thanks technosaurus
I added the tick marks like in big_bass example and this also works to pass on command.

I have saved all these example to a file, that way i can reference back to them and not forget.
cheers everyone. 8)

Posted: Fri 18 Feb 2011, 14:54
by big_bass
Hey stu90
now here is a simple way (the other way I gave you was more flexible for searching random values )



I was playing a bit to show how many ways you can solve the same problem
sometimes you need a full powerful flexible way sometimes a simple cut will work too
anyway having options are always nice because you may need a combination of all of the examples given by everyone


*somefile.txt has this data
cat|dog|/root/puppy-reference/pixmaps/gftp.png|elephant

Joe


Code: Select all

#!/bin/bash

OLDIFS=$IFS
IFS='|'
set `cat somefile.txt`
  
mtpaint $3 

IFS=$OLDIFS
set --
-----------------------------------------------
explained
OLDIFS=$IFS # this saves your separator

IFS='|' #change the separator to look for the pipe symbol instead

set `cat somefile.txt` # the set command sets the stings a number value in the list it sees them example echo $3,$4,$5 from the file you ran cat on this replaces awk and allows echo instead

mtpaint $3 #mtpaint is the app I want to run and using the third string from cat using an icon from /root/puppy-reference/pixmaps/gftp.png

IFS=$OLDIFS #reset the original separator so that bash will continue using spaces

set --
# unset the variables so arguments can be used later in the script

Posted: Sun 27 Feb 2011, 02:05
by big_bass
First of all I consider this post solved but
I stumbled on another way to run commands in an array
this would be a good place to show the example

since many people thought about different ways to solve the problem given

how to run a command and save it in an array



somefile2.txt has this data
the|three|little|pigs

Code: Select all

# run a command and save it in an array
IFS='|'
array=( $(cat<somefile2.txt) )
IFS=$' \t\n'
# if you want to see the output of any string in the array add this
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}






-----------------------------------------------------------------------------------------
now with an icon and mtpaint
------------------------------------------------------------------------------------------


somefile.txt has this data
cat|dog|/root/puppy-reference/pixmaps/gftp.png|elephant

Code: Select all

# run a command and save it in an array
IFS='|'
array=( $(cat<somefile.txt) )
IFS=$' \t\n'
mtpaint ${array[2]}


# if you want to see the output of any string in the array add this
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}


*this is flexible for any command change the cat command for ls or some other bash command in the path

Posted: Sun 27 Feb 2011, 11:58
by stu90
Hi Big_bass, thanks for the updates 8)

I have already been able to use the information i learned in this thread several times, very handy!

As they are such a big part of Linux maybe the puppy forum needs a dedicated section to scripts and commands :wink:

cheers.