LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2011, 02:08 PM   #1
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Rep: Reputation: 0
bash shell script read file word by word.


I have a output file look like this:
{"test1" : "test2", "test3" : "test4"},

How can I read word by word in each line?

This is not working code:
a=0
while read word
do a=$(($a+1));
echo $a, $word;
done < "output"
 
Old 01-21-2011, 02:46 PM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Greetingz!

If this is a shell script (such as bash), try this;
Code:
awk -F":" '{print $1,"\n",$2,"\n",$3,"\n",$4}' /path/to/the/filename
If this isn't a shell script, please provide more information on what scripting/programming language you are using, and your Linux/UNIX distribution.
(Please note if this script is going to run on more than one Linux/UNIX distribution, sometimes POSIX or other standards are not fully/correctly implemented).

Have an awesome day!
 
Old 01-21-2011, 03:25 PM   #3
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Yes, I have used bash script.
Can you write bash script instead?
Thanks,
 
Old 01-21-2011, 03:34 PM   #4
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Quote:
Originally Posted by justina View Post
Yes, I have used bash script.
Can you write bash script instead?
Thanks,
Sure, here you go;
Code:
#!/usr/bin/bash
if [ -x /bin/awk ]; then
    /bin/awk -F":" '{print $1,"\n",$2,"\n",$3,"\n",$4}' /path/to/the/filename
else
    printf "I can't find awk\n"
fi
 
Old 01-21-2011, 03:43 PM   #5
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
I just want $4 and how can I get it the double quote and },?
Thanks,
 
Old 01-21-2011, 03:49 PM   #6
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Quote:
Originally Posted by justina View Post
I just want $4 and how can I get it the double quote and },?
Thanks,
Well, you could just print it at the end of the line, like this;

Code:
#!/usr/bin/bash
if [ -x /bin/awk ]; then
    /bin/awk -F":" '{print $4,"\"","\}"}' /path/to/the/filename
else
    printf "I can't find awk\n"
fi
If you don't want the spaces, then you can eliminate the commas;

Code:
#!/usr/bin/bash
if [ -x /bin/awk ]; then
    /bin/awk -F":" '{print $4,"\"","\}"}' /path/to/the/filename
else
    printf "I can't find awk\n"
fi
I appreciate the quick response (as I'm about to go to sleep for the day).
If any of my posts (or any others) have helped you out, be sure to click the little scales icon next to my penguin.
(Or "Yes" to "Did you find this post helpful?")

Have a good night everybody!
 
Old 01-21-2011, 04:15 PM   #7
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Sorry xeleema to bother you again.
while read -r line
do
awk -F ":" '{print $3}'
done < out.js

I got the outcome "test3"
I just want to print test3 without quote.
How can I get it this ""?
Thanks,
 
Old 01-21-2011, 04:21 PM   #8
zomane
Member
 
Registered: Sep 2005
Location: Austria
Distribution: Debian, CentOS, OpenBSD, FreeBSD
Posts: 52

Rep: Reputation: 16
You can pipe output to sed :

Code:
/bin/awk -F":" '{print $4,"\"","\}"}' /path/to/the/filename | sed -e 's/\"//g'

Last edited by zomane; 01-21-2011 at 04:23 PM.
 
Old 01-21-2011, 04:43 PM   #9
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
{"test1" : "test2", "test3" : "test4"},
{"test1" : "test2", "test3" : "test4"} ]);
How can I get it } and , and ]);
Thanks,
 
Old 01-21-2011, 05:04 PM   #10
zomane
Member
 
Registered: Sep 2005
Location: Austria
Distribution: Debian, CentOS, OpenBSD, FreeBSD
Posts: 52

Rep: Reputation: 16
If I understand correct you want to remove } , ] ?
If yes you can do it like this :

Code:
/bin/awk -F":" '{print $4,"\"","\}"}' /path/to/the/filename | perl -ne 's/[\}\,\"\]]//g; print $_'
This regex will remove any } , " ] .

Last edited by zomane; 01-21-2011 at 05:06 PM.
 
1 members found this post helpful.
Old 01-21-2011, 05:19 PM   #11
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks Zomane and Xellema. Problem solved.
 
Old 01-22-2011, 02:12 AM   #12
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
Hi,

I can't believe that : putting an awk under a bashbang does not make a bash script!
it makes a shell script

Here is a full-bash script:
Code:
#!/bin/bash

while IFS='{:,}' read -a array
do
   item="${array[3]# \"}"
   echo "${item%\"*}"
done < /path/to/filename
it's bash, but it could be any POSIX compliant shell that can handle arrays.
was that that hard ?

and the last one comes with an awk and a sed or a perl: even more silly!
 
Old 01-22-2011, 03:20 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So awk does not require sed or perl to help it out seeing it already has all that it needs to process this query:

I see 2 solutions -

1. to use FS (here you need to be aware that as delimiter is at the front the the fields you want will be position + 1)
Code:
awk -F"[{}:,\" ]+" '{print $3}' file
2. Use gsub to remove guff:
Code:
awk '{gsub( /[{}:,"]/,"");print $2}' file
Both of these will print the same item.

As an alternate for bash only:
Code:
#!/bin/bash

while read -r LINE
do
    set - ${REPLY//[\{\}:,\"]/}

# now simply call parameter position
    echo $2
done<file
 
Old 01-22-2011, 03:52 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by grail;4233823
#!/bin/bash

while read -r LINE
do
set - ${REPLY//[\{\}:,\"
/}

# now simply call parameter position
echo $2
done<file
[/code]
why REPLY and not LINE?
 
Old 01-22-2011, 05:02 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
That would be because I am a numpty and when I did it in my script I just used REPLY and then added line to this one for a reason I cannot explain
So it should just be:
Code:
#!/bin/bash

while read -r
do
    set - ${REPLY//[\{\}:,\"]/}

# now simply call parameter position
    echo $2
done<file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i read two files word by word at a time using any loop by shell script? vaibhavs17 Programming 16 03-19-2010 03:48 AM
word by word comparison in two files using loop in shell script vaibhavs17 Programming 2 03-05-2010 07:41 AM
URGENT----shell script to change word in file raghupal Programming 14 10-21-2008 12:49 AM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 01:59 AM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration