Split data into multiple lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Split data into multiple lines
# 1  
Old 07-30-2007
Split data into multiple lines

All,

I have a requirement where I will need to split a line into multiple lines.

Ex:

Input:

2ABCDEFGH2POIYUY2ASDGGF2QWERTY

Output:

2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

The data is of no fixed lenght. Only the lines have to start with 2.

How can this be done.

Thanks
KP.
# 2  
Old 07-30-2007
nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
2ABCDEFGH2POIYUY2ASDGGF2QWERTY
EOF
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
# 3  
Old 07-31-2007
Quote:
Originally Posted by awk
nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
2ABCDEFGH2POIYUY2ASDGGF2QWERTY
EOF
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY
not exactly....
Code:
nawk -v RS="2" 'length()==0{next}{print "2" $0}' <<EOF
> aa2ABCDEFGH2POIYUY2ASDGGF2QWERTY
> EOF
2aa
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

a bit better:
Code:
echo 'aa2ABCDEFGH2POIYUY2ASDGGF2QWERTY' | nawk 'gsub("2", "\n2")' | sed '/^$/d'
aa
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

# 4  
Old 07-31-2007
Code:
echo "2ABCDEFGH2POIYUY2ASDGGF2QWERTY" | sed -e 's/2/\
2/g'

# 5  
Old 07-31-2007
Matrix,
Your 'sed' solution displays the first line as empty:
Code:
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

Vgersh removed this line with:
Code:
sed '/^$/d'

Your solution could be modified not to generate the empty line:
Code:
echo "2ABCDEFGH2POIYUY2ASDGGF2QWERTY" | sed -e 's/\(.\)2/\1\
2/g'

Output:
Code:
2ABCDEFGH
2POIYUY
2ASDGGF
2QWERTY

# 6  
Old 07-31-2007
Quote:
Matrix,
Your 'sed' solution displays the first line as empty:
Thats a catch ! Smilie

Sorry I didn't notice that.

Thanks for correcting it ! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple lines of data?

I use this to get 8 random letters: cat /dev/urandom | tr -dc 'A-Z' | fold -w 8 | head -n 1 Result is, WLGFJFZY What I'm trying to do is get 10 lines of random letters, separated by a line and each block having ascending numbers i.e; 00 IWMTDFIM 01 KZZZCHPQ 02 YBTGFHGT 03 (4 Replies)
Discussion started by: jenny-mac
4 Replies

2. UNIX for Beginners Questions & Answers

Split file into multiple files based on empty lines

I am using below code to split files based on blank lines but it does not work. awk 'BEGIN{i=0}{RS="";}{x="F"++i;}{print > x;}' Your help would be highly appreciated find attachment of sample.txt file (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

3. Shell Programming and Scripting

Split data into multiple lines

Hi, if i have data like below: Control|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241 Im getting output in required format as below Control|AC-00011-CN-2475208 Control|AC-00011-CN-2475211 Control|AC-00007-CN-2475238 Control|AC-00007-CN-2475241 using awk... (9 Replies)
Discussion started by: JSKOBS
9 Replies

4. Shell Programming and Scripting

Extracting data from multiple lines

Hi All, I am stuck in one step.. I have one file named file.txt having content: And SGMT.perd_id = (SELECT cal.fiscal_perd_id FROM $ODS_TARGT.TIM_DT_CAL_D CAL FROM $ODS_TARGT.GL_COA_SEGMNT_XREF_A SGMT SGMT.COA_XREF_TYP_IDN In (SEL COA_XREF_TYP_IDN From... (4 Replies)
Discussion started by: Shilpi Gupta
4 Replies

5. Shell Programming and Scripting

Split a line into multiple lines based on delimeters

Hi, I need help to split any lines that contain ; or , input.txtAc020 Not a good chemical process AC030 many has failed, 3 still maintained AC040 Putative; epithelial cells AC050 Predicted binding activity AC060 rodC Putative; upregulated in 48;h biofilm vs planktonic The output... (8 Replies)
Discussion started by: redse171
8 Replies

6. Shell Programming and Scripting

Split: File into multiple and keeping the same 3 lines from input into all output files

The following code will split the infile into multiple files. However, I need it to insert the same first 3 lines from the original input file into each splitted file. How do I modify my script below to do so: print -n "Enter file name to split? " ; read infile if then echo "Invalid file... (4 Replies)
Discussion started by: mrn6430
4 Replies

7. Shell Programming and Scripting

Split the single file lines into multiple files

Let's assume that I have a file name called ‘A' and it has 100 lines in it and would like to split these 100 lines into 4 files as specified bellow. INPUT: Input file name A 1 2 3 4 5 6 7 8 9 ........100 Output: 4 output files (x,y,z,w) File x should contains (Skip 4 lines)... (15 Replies)
Discussion started by: subbarao25
15 Replies

8. Shell Programming and Scripting

How do you use pull data from multiple lines to do a for statement?

Guys I am having a problem with being able to find missing monitors in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the pool... (7 Replies)
Discussion started by: scottzx7rr
7 Replies

9. Shell Programming and Scripting

Outputting data from multiple lines

Hi guys, looking for a bit of advise, and as I am a complete novice, please excuse the daft questions!! I have a list of events and of which entry looks like this; # # Event 1 # NAME = Event 1 # 12345 : 123 : 1 : 1 : L,1,N : 1,0 : Event # # Event 2 # NAME = Event 2 # 12346... (8 Replies)
Discussion started by: JayC89
8 Replies

10. Shell Programming and Scripting

Split a huge line into multiple 120 characters lines with sed?

Hello , I'm trying to split a file which contains a single very long line. My aim is to split this single line each 120 characters. I tried with the sed command : `cat ${MYPATH}/${FILE}|sed -e :a -e 's/^.\{1,120\}$/&\n/;ta' >{MYPATH}/${DEST}` but when I wc -l the destination file it is... (2 Replies)
Discussion started by: jerome_1664
2 Replies
Login or Register to Ask a Question