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" |
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 (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! |
Yes, I have used bash script.
Can you write bash script instead? Thanks, |
Quote:
Code:
#!/usr/bin/bash |
I just want $4 and how can I get it the double quote and },?
Thanks, |
Quote:
Code:
#!/usr/bin/bash Code:
#!/usr/bin/bash 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! |
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, |
You can pipe output to sed :
Code:
/bin/awk -F":" '{print $4,"\"","\}"}' /path/to/the/filename | sed -e 's/\"//g' |
{"test1" : "test2", "test3" : "test4"},
{"test1" : "test2", "test3" : "test4"} ]); How can I get it } and , and ]); Thanks, |
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 $_' |
Thanks Zomane and Xellema. Problem solved.
|
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 was that that hard ? and the last one comes with an awk and a sed or a perl: even more silly! |
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 Code:
awk '{gsub( /[{}:,"]/,"");print $2}' file As an alternate for bash only: Code:
#!/bin/bash |
Quote:
|
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 |
All times are GMT -5. The time now is 05:52 AM. |