Split data into multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split data into multiple lines
# 1  
Old 08-17-2015
Split data into multiple lines

Hi, if i have data like below:
Code:
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
Code:
Control|AC-00011-CN-2475208
Control|AC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AC-00007-CN-2475241

using
Code:
awk '{gsub(/[|,]/," ");for (i=2;i<=NF;i++)print $1,$i}' OFS="|" test.txt

but im getting data like below(without spaces):
Code:
Control|AC-00011-CN-2475208AC-00011-CN-2475211AC-00007-CN-2475238AC-00007-CN-2475241

Please help me to get output in required format in this case

Last edited by vbe; 08-17-2015 at 09:23 AM..
# 2  
Old 08-17-2015
Seems to work for me:
Code:
awk '{gsub(/[|,]/," ");for (i=2;i<=NF;i++)print $1,$i}' OFS="|"  file
Control|AC-00011-CN-2475208
Control|AC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AC-00007-CN-2475241

Sure the input file has spaces as field separators?
# 3  
Old 08-17-2015
im not getting Spaces in between, thats the problem Smilie
Data is coming as below

Quote:
Control|AC-00011-CN-2475208TIC-00011-CN-2475211AC-00007-CN-2475238AML-00007-CN-2475241
Required output
Control|AC-00011-CN-2475208
Control|TIC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AML-00007-CN-2475241
# 4  
Old 08-17-2015
If I understand your question right, you want the data that is in below format:

Code:
Control|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241

To appear as below format:


Code:
Control|AC-00011-CN-2475208 
Control|AC-00011-CN-2475211 
Control|AC-00007-CN-2475238 
Control|AC-00007-CN-2475241

If yes, here's what you could use:

Code:
[root@enolin11pl di4]# cat JSKOBS.txt
Control|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241
[root@enolin11pl di4]# awk '{print $1;for (i=2;i<=NF;i++) {printf"Control|";print $i}}' JSKOBS.txt
Control|AC-00011-CN-2475208
Control|AC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AC-00007-CN-2475241
[root@enolin11pl di4]#

# 5  
Old 08-17-2015
Quote:
Originally Posted by JSKOBS
Hi, if i have data like below:
Code:
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
Code:
Control|AC-00011-CN-2475208
Control|AC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AC-00007-CN-2475241

using
Code:
awk '{gsub(/[|,]/," ");for (i=2;i<=NF;i++)print $1,$i}' OFS="|" test.txt

but im getting data like below(without spaces):
Code:
Control|AC-00011-CN-2475208AC-00011-CN-2475211AC-00007-CN-2475238AC-00007-CN-2475241

Please help me to get output in required format in this case
Hello JSKOBS,

Could you please try following and let me know if this helps you.
Code:
 awk -F"AC-" '{for(i=2;i<=NF;i++){print $1 FS $i}}'  Input_file

Output will be as follows.
Code:
Control|AC-00011-CN-2475208
Control|AC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AC-00007-CN-2475241

Thanks,
R. Singh
# 6  
Old 08-17-2015
Given the substrings you try to extract are of constant length, try
Code:
awk '{ST=1; while (ST < 76) {print $1, substr($2, ST, 19); ST+=19}}' FS="|" OFS="|"  file
Control|AC-00011-CN-2475208
Control|AC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AC-00007-CN-2475241

---------- Post updated at 15:10 ---------- Previous update was at 15:07 ----------

Ohhh - I see only now your second sample file has a different structure. Please tell us how to identify the substring boundaries.
# 7  
Old 08-17-2015
Thanks all for the reply.
The file has different structure, it is not coming with just AC-*
the substring has 4 words in it(seperated by "-")

actual file as below
Quote:
Control|AC-00011-CN-2475208TIC-00011-CN-2475211AC-00007-CN-2475238AML-00007-CN-2475241
im just manully giving spaces so we can understand the substrings (each substring has 4 words):
Quote:
Control|AC-00011-CN-2475208 TIC-00011-CN-2475211 AC-00007-CN-2475238 AML-00007-CN-2475241
output should be:
Quote:
Control|AC-00011-CN-2475208
Control|TIC-00011-CN-2475211
Control|AC-00007-CN-2475238
Control|AML-00007-CN-2475241
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

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

4. 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

5. 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

6. 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

7. 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

8. 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

9. UNIX for Dummies Questions & Answers

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. (5 Replies)
Discussion started by: kingofprussia
5 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