[Solved] Search .txt file for random strings
[Solved] Search .txt file for random strings
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.
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.
Last edited by stu90 on Sun 27 Feb 2011, 12:00, edited 1 time in total.
Code: Select all
cat file.txt | awk 'BEGIN { FS="|" } { print $1, $2, $3, $4 }'
FS="|" set the field separator to |
$1 = first word
$2 = second word
etc.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]
-
- Posts: 76
- Joined: Fri 23 Jan 2009, 14:59
- Location: California
Re: Search .txt file for random string between two fixed str
# echo "hi|there|how|are|you">temp.txtstu90 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
# 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
#
Hey stu90
so here is another option
Joe
lets say this is the content of somefile.txt
cat|dog|fish|elephant
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
the best way is what is easy fast and you understand how to make edits to itrandom|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?
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,big_bass wrote:Hey stu90
the best way is what is easy fast and you understand how to make edits to itrandom|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?
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
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.
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
there are very small tick marks enclosing everything
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 }'`
Cracking, thanks Joe.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
there are very small tick marks enclosing everythingCode: Select all
mtpaint `cat somefile.txt | grep "|" | tr '|' ' ' | awk '{ print $3 }'`
I think this is another way as well
Code: Select all
name1=$(cat /tmp/test | grep "|" | tr '|' ' ' | awk '{ print $1 }') && /usr/bin/viewnior $name1
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
or you can use
-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)
Code: Select all
cut -s -d "|" -f 3 $file
-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)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
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
-----------------------------------------------
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
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
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
-----------------------------------------------------------------------------------------
now with an icon and mtpaint
------------------------------------------------------------------------------------------
somefile.txt has this data
cat|dog|/root/puppy-reference/pixmaps/gftp.png|elephant
# 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
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