LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Splitting horizontal to verical...lines (https://www.linuxquestions.org/questions/linux-general-1/splitting-horizontal-to-verical-lines-566270/)

venki 07-03-2007 06:49 AM

Splitting horizontal to verical...lines
 
HI i have a file like this ...

"Inter","Hyd bad","General ",,"http://nal.html",
"Inter","HYd bad","ca ",,"http://ca.html",
"Inter","Hyd bad","cas ",,"http:cass.html",

there are 1000 lines like this .
i want to make every horizantal line into vertical line.
then the output should be
"Inter",
"Hyd bad",
"General ",
"http://nal.html",
....

plz help me...give the bash script for the following

pixellany 07-03-2007 07:17 AM

To change the double comma to a single:
sed 's/,,/,/g'

to replace all the commas with linefeeds:
sed 's/,/\n/g'

Pipe these together (after cat filename), and you'll get the "vertical list" with one blank line after each set.

To remove the last comma so this does not happen:
sed 's/,$//g'

ghostdog74 07-03-2007 09:39 AM

Code:

awk '{gsub(/,+/,",\n")}{print}' file

venki 07-04-2007 02:25 AM

Thks pixellany and ghostdog74 , it is working perfectly

timmeke 07-04-2007 04:40 AM

sed is for stream editing. awk is overkill too. Just use "tr". It takes care of transliteration, prevent double linefeeds, etc.

Code:

tr -s "," "\n" your_horiz_file
should suffice. Simple, no?

pixellany 07-04-2007 09:22 AM

If you just want to do one task, use the first tool that works. If you want to learn programming, try ALL of the tools
Quote:

tr -s "," "\n" your_horiz_file
Doesn't that put two linefeeds whereever there is the double comma?

timmeke 07-04-2007 09:48 AM

@pixellany: no it doesn't. The -s option squashes duplicates, triples, quadruples, etc. See "man tr".

ghostdog74 07-04-2007 10:24 AM

Quote:

Originally Posted by timmeke
sed is for stream editing. awk is overkill too. Just use "tr". It takes care of transliteration, prevent double linefeeds, etc.

Code:

tr -s "," "\n" your_horiz_file
should suffice. Simple, no?

for OP's case, it doesn't matter. sed/awk/tr whatever. They did the job.
by the way, the tr solution should be
Code:

tr -s "," "\n" < file
also noticed that OP's sample output included the "," at the end, ie it is not tr'ed to "\n".


All times are GMT -5. The time now is 09:54 AM.