[Solved] Search .txt file for random strings

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
stu90

[Solved] Search .txt file for random strings

#1 Post 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.
Last edited by stu90 on Sun 27 Feb 2011, 12:00, edited 1 time in total.

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#2 Post 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.
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]

ken geometrics
Posts: 76
Joined: Fri 23 Jan 2009, 14:59
Location: California

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

#3 Post 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
#

stu90

#4 Post by stu90 »

Excellent - thanks guys! 8)

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#5 Post 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

stu90

#6 Post 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.

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#7 Post 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

stu90

#8 Post 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

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#9 Post 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)
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].

stu90

#10 Post 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)

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#11 Post 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

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#12 Post 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

stu90

#13 Post 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.

Post Reply