break the string and print it in a new line after a specific word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting break the string and print it in a new line after a specific word
# 1  
Old 08-28-2011
Question break the string and print it in a new line after a specific word

Hi Gurus
I am new to this forum.. I am using HP Unix OS.

I have one single string in input file as shown below
Code:
Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........

I want to achive the result in a output file as shown below
Code:
Abc123 | cde | fgh | ghik| lmno 
Abc456 |one |two |three | four 
Abc789 | five | Six | seven | eight 
Abc098 | ........

I used the command shown below
Code:
cat input.txt | sed 's/PC/~PC/g' | tr -s '~' '\n' | sed '/^$/d' | sed 's/ *$//' > output.txt

it works in Sun Solaris, but not working in HP Unix(the output file is coming blank Smilie) not sure whether i am doing anything wrong


Any help is greatly appreciated

Thanks

Last edited by Franklin52; 08-28-2011 at 09:20 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-28-2011
try this:

Code:
echo "Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight" |fold -s -w 34

or

Code:
echo "Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight" | awk -F"|" 'BEGIN{ORS=""} {for(i=1;i<=NF;i++) {if((i%5)!=0) {print $i"|"}else {print $i"\n"}}}'


Last edited by dude2cool; 08-28-2011 at 02:13 AM..
# 3  
Old 08-28-2011
Code:
echo 'Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three |'\
' four | Abc789 | five | Six | seven | eight | Abc098 | ....' |     
perl -lpe 's{\|}{++$i % 5 ? "|" : "\n"}eg; s/^ *//mg'
Abc123 | cde | fgh | ghik| lmno 
Abc456 |one |two |three | four 
Abc789 | five | Six | seven | eight 
Abc098 | ....

# 4  
Old 08-28-2011
Code:
$ echo $string | awk '{gsub(/\|[ \t]*Abc/,"\nAbc");}1'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. Shell Programming and Scripting

Print String Every Specific Line

Dear All, I have input file like this, 001 059 079 996 758 079 069 059 079 ... ... Desired output: AA 001 BB 059 (4 Replies)
Discussion started by: attila
4 Replies

5. Shell Programming and Scripting

Line break on word

I have a file that contains the following: ^field LINE_1 data ^field LINE_2 data ^field LINE_3 data ^field LINE_4 data ^field LINE_5 data ... And im looking to do a line break at the end of the number before the text to make it look like this ^field LINE_1 ... (11 Replies)
Discussion started by: darbs121
11 Replies

6. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

7. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Shell Programming and Scripting

sed: break before word if it's not last on the line

I've been trying this, and can't get it right. I want to put a line break before a word, but only if it's *not* the last word in the line. So if the break work was "fish," then... We want to fish tomorrow ...would become... We want to fish tomorrow ...but this line would remain... (3 Replies)
Discussion started by: estebandido
3 Replies

10. Shell Programming and Scripting

search a word and print specific string using awk

Hi, I have list of directory paths in a variable and i want to delete those dirs and if dir does not exist then search that string and get the correct path from xml file after that delete the correct directory. i tried to use grep and it prints the entire line from the search.once i get the entire... (7 Replies)
Discussion started by: dragon.1431
7 Replies
Login or Register to Ask a Question