Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 30 Jul 2014, 01:09
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
[Solved] Search .txt file for random strings
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [13 Posts]  
Author Message
stu90


Joined: 25 Feb 2010
Posts: 1401
Location: England. Dell Inspiron 1501. Dpup

PostPosted: Thu 17 Feb 2011, 09:54    Post subject:  [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.

Last edited by stu90 on Sun 27 Feb 2011, 08:00; edited 1 time in total
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2048
Location: The Blue Marble

PostPosted: Thu 17 Feb 2011, 10:17    Post subject:  

Code:
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, Slacko and Puppeee user. Puppy user since 2.13
Back to top
View user's profile Send private message 
ken geometrics

Joined: 23 Jan 2009
Posts: 76
Location: California

PostPosted: Thu 17 Feb 2011, 11:02    Post subject: Re: Search .txt file for random string between two fixed str  

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
#
Back to top
View user's profile Send private message 
stu90


Joined: 25 Feb 2010
Posts: 1401
Location: England. Dell Inspiron 1501. Dpup

PostPosted: Thu 17 Feb 2011, 11:31    Post subject:  

Excellent - thanks guys! Cool
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Thu 17 Feb 2011, 11:34    Post subject:  

Hey stu90

Quote:

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:

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

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
stu90


Joined: 25 Feb 2010
Posts: 1401
Location: England. Dell Inspiron 1501. Dpup

PostPosted: Thu 17 Feb 2011, 12:59    Post subject:  

big_bass wrote:
Hey stu90

Quote:

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:

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. Smile

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.
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Thu 17 Feb 2011, 14:18    Post subject:  

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:
mtpaint `cat somefile.txt | grep "|" | tr '|' ' ' | awk '{ print $3 }'`


there are very small tick marks enclosing everything

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
stu90


Joined: 25 Feb 2010
Posts: 1401
Location: England. Dell Inspiron 1501. Dpup

PostPosted: Thu 17 Feb 2011, 14:32    Post subject:  

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:
mtpaint `cat somefile.txt | grep "|" | tr '|' ' ' | awk '{ print $3 }'`


there are very small tick marks enclosing everything


Cracking, thanks Joe. Cool

I think this is another way as well
Code:
name1=$(cat /tmp/test | grep "|" | tr '|' ' ' | awk '{ print $1 }') && /usr/bin/viewnior $name1


cheers
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4284

PostPosted: Thu 17 Feb 2011, 15:41    Post subject:  

or you can use
Code:
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)

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
stu90


Joined: 25 Feb 2010
Posts: 1401
Location: England. Dell Inspiron 1501. Dpup

PostPosted: Thu 17 Feb 2011, 23:22    Post subject:  

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. Cool
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Fri 18 Feb 2011, 10:54    Post subject:  

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:
#!/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

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Sat 26 Feb 2011, 22:05    Post subject:  

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:

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

# 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

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
stu90


Joined: 25 Feb 2010
Posts: 1401
Location: England. Dell Inspiron 1501. Dpup

PostPosted: Sun 27 Feb 2011, 07:58    Post subject:  

Hi Big_bass, thanks for the updates Cool

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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [13 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0913s ][ Queries: 12 (0.0067s) ][ GZIP on ]