1828305 Members
3120 Online
109975 Solutions
New Discussion

Re: Sed??

 
SOLVED
Go to solution
Rinky
Advisor

Sed??

Hi,

I have a comma seperated file. The second field in it is a description field.
But has new line character within the field.
I want the new line character to be removed so that the description field comes in the same line.

For eg: In the file if it is:
10000,"Today
is a holiday"

I want it to be
10000,"Today is a holiday"

Please help..
16 REPLIES 16
Dennis Handly
Acclaimed Contributor
Solution

Re: Sed??

You'll have to explain in more detail. Are ALL lines broken up? Or only some? Or do you need to look for a mismatched "?
Or any line with a " is mismatched?

If the lines are always broken in pairs, then you could possibly use sed. But if you have to check, you need awk:
awk '
{
inx = index($0, "\"")
if (inx != 0) {
save = $0
getline
print save, $0
next
}
print
}' file
Rinky
Advisor

Re: Sed??

Ok..
Only some lines have this problem. Probably a search for mismathced " will do in a line..
how do we accomplish tat?
Marcin O.
Frequent Advisor

Re: Sed??

Hello

If all lines are broken use edit file with vi editor, enter :g/./join

Regards
Marcin
Dennis Handly
Acclaimed Contributor

Re: Sed??

>Probably a search for mismathced " will do in a line.. how do we accomplish that?

This assumes that the second field has quotes, not the first nor any other.
awk '
{
inx = index($0, "\"")
if (inx != 0) {
rest = substr($0, inx + 1) # after quote
inx = index(rest, "\"")
if (inx == 0) { # mismatched
save = $0
getline
print save, $0 #join lines
next
}
}
print
}' file

I suppose if you had an arbitrarily complex case you could set the delimiter to ", then if the number of fields was even, then join lines.
Marcin O.
Frequent Advisor

Re: Sed??

If all lines start from numeric and only broken line have to be processed you may use in vi

:v/^[0-9]/-1j

and only broken line will be connected

Regards
Marcin
Dennis Handly
Acclaimed Contributor

Re: Sed??

>Marcin may use in vi
:v/^[0-9]/-1j
>and only broken line will be connected

I'm at a loss on how to get that to work and why it would work.
It seems you are executing the ex(1) visual command from the vi ":" prompt??

Or did you mean "g" instead of "v"?
Ah, you're using a foreign devil vim instead of vi. And "v" did work but I still don't know why.
Marcin O.
Frequent Advisor

Re: Sed??

Hi Dennis

I using standard hp-ux vi.
And there is no mistake with "v", "v" - inverts the search, ex. ":v/./d" = ":g/^$/d" (delete all empty lines)

Regards
Marcin
Dennis Handly
Acclaimed Contributor

Re: Sed??

>Marcin: I using standard hp-ux vi.

Hmm, it works now. I don't know what I did to get it to fail. ??

>And there is no mistake with "v", "v" - inverts the search,

You can only find it in ex(1) if you know v is related to g or global. They don't list it alphabetically under v. And don't have flashing red lights. :-(
It is in the Abbreviations box.

Thanks for the info. Though if you used g!, it would have been easier to figure it out.
Marcin O.
Frequent Advisor

Re: Sed??

Rinky, please assign points.
Hein van den Heuvel
Honored Contributor

Re: Sed??

If not resolved yet, then this perl should do it. It just checks field 2 as written:

perl -pe 'if (!/^.*?,".*?"/ && !eof) { chomp; $_ .=<> }' old > new

To check any field (including 2), leave the begin-of-line anchor of:


perl -pe 'if (!/,".*?"/ && !eof) { chomp; $_ .=<> }' old > new

fwiw,
Hein.


James R. Ferguson
Acclaimed Contributor

Re: Sed??

Hi Rinky:

Another Perl:

# perl -ni.old -e 'if (!m/"$/) {chomp;print $_." "} else {print}' file

This will also update your file in situ (in place) leaving an unmodified copy as "file.old".

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Sed??

Try the sed construct below:

# sed 'N;s/\n/ /p' file
Dennis Handly
Acclaimed Contributor

Re: Sed??

>Sandman: Try the sed construct below:
# sed 'N;s/\n/ /p' file

This was rejected as not every line had the problem.
Sandman!
Honored Contributor

Re: Sed??

>Only some lines have this problem. Probably a search for mismathced " will
>do in a line..

Good point Dennis...hadn't read the entire thread until now and based on requirements try the construct below which might be what Rinky's looking for:

# sed '/"$/!N;s/\n/ /p' file
Rinky
Advisor

Re: Sed??

Thanks everybody:)
Rinky
Advisor

Re: Sed??

Got wat i wanted!