Awk/Sed - appending within file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk/Sed - appending within file
# 15  
Old 03-22-2011
Quote:
Originally Posted by yinyuemi
how about this?
Code:
awk '/\"/&&$0!~/,/{++p}{printf (p%2?$0" ":$0"\n")}' file
 
"one":"field one",
"two":"field two",
"three":"field three line 1 field three line 2 field three line 3"

That works great on that example, it doesn't quite work on my actual file (which I can't actually post unfortunately, without subbing the data out). If we can break down the parts I'd be grateful, e.g.
Code:
/\"/&&$0!~/,/

I take the && to be AND, the $0 to refer to a whole line and !~ to be 'does not match'. So is this line identifying lines that start with a quote and do not end in ',' ? i.e. skipping lines 1 & 2?

If that's the case, unfortunately I can't use that as my actual 'field three' is the content of an article, and individual lines could conceivably end in a comma (and could even end in '",' id someone is being quoted).

Perhaps another way around this is to find an equivalent of xml's CDATA tags - a way to wrap the 'field three' text in a way that the contents are just ignored, and are allowed to pass JSON syntax rules. I don't know if that equivalent exists.

Back to the syntax:
Code:
{++p}{printf (p%2?$0" ":$0"\n")}

are these two separate statements, executed in sequence? it seems p is a var being incremented from I assume 0. The next statement is doing a mod (p%2) that I guess is deciding when to concatenate the lines, but that's where the syntax loses me.

If anyone can clarify this I'd be grateful. Thanks again for posting, yinyuemi, I do appreciate it! Smilie

Last edited by Franklin52; 03-23-2011 at 11:12 AM.. Reason: Please use code tags
# 16  
Old 03-22-2011
Please try this way, hope it helps:
Code:
awk 'gsub("\"","\"",$0)%2{++p}{printf (p%2?$0" ":$0"\n")}' file
"one":"field one",
"two":"field two",
"three":" field three line 1  field three line 2  field three line 3 "

# 17  
Old 03-22-2011
That's nearly it, except I need the new lines to be replaced with the explicit text '\n' in field three (so that later on, they can be replaced with <BR>). Also, do you know how I could combine that statement with sed in order to output to the original file?

In investigating the print command I found that this:
Code:
awk '{printf ($0)}' testfile.xml

nearly gives me what I want:
Code:
"one":"field one","two":"field two","three":"field three line 1 field three line 2 field three line 3"

except I need:
Code:
"one":"field one","two":"field two","three":"field three line 1 \nfield three line 2 \nfield three line 3"

N.B. I know I suggested before that I was after:
Code:
"one":"field one",
"two":"field two",
"three":"field three line 1 \nfield three line 2 \nfield three line 3"

but in fact it doesn't matter if the new line occurs at the end of a complete line (lines 1 & 2), that is OK in terms of JSON. It's when the new line occurs within a set of "" that it chokes

Last edited by Franklin52; 03-23-2011 at 11:14 AM.. Reason: Please use code tags
# 18  
Old 03-22-2011
Code:
awk 'gsub("\"","\"",$0)%2{++p}NF{if(/\"/) {printf (p%2?$0" ":$0" \n")} else {printf (p%2?$0"\\n":$0"\n")}}' file
"one":"field one",
"two":"field two",
"three":" field three line 1 \nfield three line 2 \nfield three line 3 \n"

This User Gave Thanks to yinyuemi For This Post:
# 19  
Old 03-22-2011
I think that's it! Thank you so much! It's a complex line and I'm going to put the time in to understand it. One last question is how I could combine it with sed so I can have the line in a bash script writing to the same file. I tried:
Code:
sed -i '' "`awk 'gsub("\"","\"",$0)%2{++p}NF{if(/\"/) {printf (p%2?$0" ":$0" \n")} else {printf (p%2?$0"\\n":$0"\n")}}' $filename `/" $filename

but that's not it...

Anyway, thanks again. As I said originally, I am really up against it timewise, and you have helped very much.

Last edited by Franklin52; 03-23-2011 at 11:14 AM.. Reason: Please use code tags
# 20  
Old 03-23-2011
Hi Singerfc,

I also have no idea about it, but you can try what I suggest you before by generating a temp file, then "cat" it to replace the orignal file.

Best,

Y
# 21  
Old 03-23-2011
no problem, yeah I'm trying that with the bash $RANDOM var for the temp file, as the operation I'm coding for could be happening often, and possibly at the same time.

thanks again, if I keep learning maybe I can help you one day Smilie

---------- Post updated 03-23-11 at 12:26 AM ---------- Previous update was 03-22-11 at 11:40 PM ----------

argh, as always there is one last detail: I need to delimit quotes (") in that third field, as in:

"field":"he said \"hello\""

as always, help appreciated
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading and appending a row from file1 to file2 using awk or sed

Hi, I wanted to add each row of file2.txt to entire length of file1.txt given the sample data below and save it as new file. Any idea how to efficiently do it. Thank you for any help. input file file1.txt file2.txt 140 30 200006 141 32 140 32 200006 142 33 140 35 200006 142... (5 Replies)
Discussion started by: ida1215
5 Replies

2. UNIX for Dummies Questions & Answers

Appending columns at the end of output using awk/sed

Hi , I have the below ouput, =====gopi===== assasassaa adsadsadsdsada asdsadsadasdsa sadasdsadsd =====kannan=== asdasdasd sadasddsaasd adasdd =====hbk=== asasasssa .... .. I want the output like as below, not able paste here correctly. (2 Replies)
Discussion started by: eeegopikannan
2 Replies

3. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

4. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

5. Shell Programming and Scripting

appending a file using sed in ksh

i am trying to append a 5 line SGML file(file1) with a 500,000 line SGML file (file2). file1 is a template, so i wish to preserve. i only want to add lines 5 to the end of file2. i have: cp file1 temp1 sed -n '5,$p' file2 >> temp1 when i check the tail of temp1, i consistantly find the... (3 Replies)
Discussion started by: smac
3 Replies

6. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies

7. Shell Programming and Scripting

appending and sed

Hello, I want to add string #REAL at the end of all lines that contain real numbers. How to do this using sed ? (1 Reply)
Discussion started by: scotty_123
1 Replies

8. Shell Programming and Scripting

appending to sed output of one file into the middle of file

hi, i have a file file1 file2 ----------- ----------------- aa bbb ccc 111 1111 1111 ddd eee fff 222 3333 4444 ggg hhh... (5 Replies)
Discussion started by: go4desperado
5 Replies

9. Shell Programming and Scripting

Appending string to xml file using "sed"

Hi folks, Following a section in opmn.xml file: </process-type> <process-type id="OC4J_RiGHTv_IRD1" module-id="OC4J"> <environment> <variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/> <variable id="SHLIB_PATH"... (2 Replies)
Discussion started by: nir_s
2 Replies

10. UNIX for Dummies Questions & Answers

SED Question -- on appending to a file

:confused: I have a script that Cats a flat database file which contains 12 columns into sed. I want to add a 13th column which includes " ,2005-08-29 " * The date needs to be the current date. This 13th column would be appended to the end of each line. Does anyone have a clue... (5 Replies)
Discussion started by: Redg
5 Replies
Login or Register to Ask a Question