I have searched high and low and hope you can help me. I need to find each line in a file which does NOT begin with a double quote (") and append that line to the previous line.
I have been successful doing this using the following command:
cat filname.csv | sed -e :a -e '$!Ns/\n[^"]//;ta -e 'P;D' > newfilename.csv
My issue is the substitution. As you would expect after the line is appended to th开发者_开发知识库e previous line the first character is removed. I need it to not be removed. I tried cat filname.csv | sed -e :a -e '$!Ns/\n[^"]/&/;ta -e 'P;D' > newfilename.csv
but it just hangs. I thought the ampersand (&) would copy the matched line.
Input:
"line 1
<line 2>
Output with existing or first sed command is:
lineline2>
** Note the removal of the <
I need the output to be line1<line2>
Any help you can provide would be GREATLY appreciated!!
I can't seem to get your original command to work. Did you leave out a single-quote somewhere?
Assuming that the original command works and that I remember how sed works, I would just use backreferences, like so:
sed -e :a -e '$!Ns/\n([^"])/\1/;ta -e 'P;D'
(added parentheses and replaced & with \1)
You're missing the closing single quote so the shell interprets the ampersand as a request to background the operation. Try:
sed -e ':a' -e '$!Ns/\n\([^"]\)/\1/;ta' -e 'P;D' filname.csv > newfilename.csv
You can't use an ampersand anyway, because that would keep the newline.
You could also do it with awk:
awk '{if (NR>1 && $1 ~ "^\"") print "" ; printf $0 } END {print ""}' filname.csv > newfilename.csv
$ cat file
"line 1
<line 2>
"line 3
<line 4>
$ awk 'ORS=/^\042/?" ":"\n"' file
"line 1 <line 2>
"line 3 <line 4>
or if you could use Ruby(1.9+)
$ ruby -ne 'print /^"/? $_.chomp: $_' file
"line 1<line 2>
"line 3<line 4>
精彩评论