LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash shell script read file word by word. (https://www.linuxquestions.org/questions/programming-9/bash-shell-script-read-file-word-by-word-857845/)

justina 01-21-2011 02:08 PM

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"

xeleema 01-21-2011 02:46 PM

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!

justina 01-21-2011 03:25 PM

Yes, I have used bash script.
Can you write bash script instead?
Thanks,

xeleema 01-21-2011 03:34 PM

Quote:

Originally Posted by justina (Post 4233378)
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


justina 01-21-2011 03:43 PM

I just want $4 and how can I get it the double quote and },?
Thanks,

xeleema 01-21-2011 03:49 PM

Quote:

Originally Posted by justina (Post 4233407)
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!

justina 01-21-2011 04:15 PM

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,

zomane 01-21-2011 04:21 PM

You can pipe output to sed :

Code:

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

justina 01-21-2011 04:43 PM

{"test1" : "test2", "test3" : "test4"},
{"test1" : "test2", "test3" : "test4"} ]);
How can I get it } and , and ]);
Thanks,

zomane 01-21-2011 05:04 PM

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 } , " ] .

justina 01-21-2011 05:19 PM

Thanks Zomane and Xellema. Problem solved.

everToulouse 01-22-2011 02:12 AM

Hi,

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

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!

grail 01-22-2011 03:20 AM

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


ghostdog74 01-22-2011 03:52 AM

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?

grail 01-22-2011 05:02 AM

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 :redface:
So it should just be:
Code:

#!/bin/bash

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

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



All times are GMT -5. The time now is 05:52 AM.