Split a line into next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a line into next line
# 8  
Old 04-09-2014
Quote:
Originally Posted by krupasindhu18
hi,

i tried with below commod, it is not working
sed '1s/./\n/34'

is there any other ways

thanks,
kk
Try the other suggestion, I corrected it...


--
@Ravindersingh... read the requirement again, the other way around; it needs to split if the first line is merged with the header
# 9  
Old 04-09-2014
Thanks Scrutinizer for correcting me, it's my bad I didn't read it correctly.

For input.

Code:
emplid|empl_name|salary|hire_date1111|gro|3000|01/01/2014
1111|ghgo|4000|01/03/2014
1111|dkguo|5000|02/01/2014
1111|ehrt|6000|01/04/2014
1111|kfgtr|8000|03/01/2014


Code:
awk 'BEGIN{print "emplid|empl_name|salary|hire_date"} gsub(/^emplid.*hire_date/,X,$0) 1' file_name


Output will be as follows.

Code:
emplid|empl_name|salary|hire_date
1111|gro|3000|01/01/2014
1111|ghgo|4000|01/03/2014
1111|dkguo|5000|02/01/2014
1111|ehrt|6000|01/04/2014
1111|kfgtr|8000|03/01/2014

When input is as follows.

Code:
emplid|empl_name|salary|hire_date
1111|gro|3000|01/01/2014
1111|ghgo|4000|01/03/2014
1111|dkguo|5000|02/01/2014
1111|ehrt|6000|01/04/2014
1111|kfgtr|8000|03/01/2014


Use following.

Code:
awk 'BEGIN{print "emplid|empl_name|salary|hire_date"} gsub(/^emplid.*hire_date/,X,$0) 1' | grep -v '^$'


Thanks,
R. Singh

Last edited by RavinderSingh13; 04-09-2014 at 07:55 AM.. Reason: correcting input
# 10  
Old 04-09-2014
Split a line into next line

Hi All,

Thanks for your reply.

From every body's reply, i am thinking one mistake with my post beacuse every one is thinking records 1st tab is fix '1111', but it is dynamic values. this records will be entered by users.

Code:
emplid|empl_name|salary|hire_date1111|gro|3000|01/01/2014
9867|ghgo|4000|01/03/2014
5342|dkguo|5000|02/01/2014
7452|ehrt|6000|01/04/2014
2937|kfgtr|8000|03/01/2014

here is only header is fixed length.

Thanks,
Kkk

Last edited by Scrutinizer; 04-09-2014 at 10:42 AM.. Reason: code tags
# 11  
Old 04-09-2014
I used the fixed length of the header (33 characters)...
# 12  
Old 04-09-2014
Code:
sed '1s/./\n&/34' kk.txt  is not working

see the output

Code:
FEEDS:/projects/feeds/rfs>sed '1s/./\n&/34' kk.txt
emplid|empl_name|salary|hire_daten1111|gro|3000|01/01/2014
9867|ghgo|4000|01/03/2014
5342|dkguo|5000|02/01/2014
7452|ehrt|6000|01/04/2014
2937|kfgtr|8000|03/01/2014


Last edited by Scrutinizer; 04-09-2014 at 11:53 AM.. Reason: code tags
# 13  
Old 04-09-2014
I know, that is why I suggested you try the other one:
Code:
sed '1s/./\
&/34' file

The backslash should be the last character on the line...

Output:
Code:
emplid|empl_name|salary|hire_date
1111|gro|3000|01/01/2014
1111|ghgo|4000|01/03/2014
1111|dkguo|5000|02/01/2014
1111|ehrt|6000|01/04/2014
1111|kfgtr|8000|03/01/2014


Last edited by Scrutinizer; 04-10-2014 at 02:20 AM.. Reason: Added output
# 14  
Old 04-09-2014
Longhand CygWin under Windows Vista, bash...
Code:
#!/bin/bash
# split_me.sh
#  CygWin
echo "emplid|empl_name|salary|hire_date1111|gro|3000|01/01/2014
9867|ghgo|4000|01/03/2014
5342|dkguo|5000|02/01/2014
7452|ehrt|6000|01/04/2014
2937|kfgtr|8000|03/01/2014" > /tmp/text
splitme=$(cat < /tmp/text)
printf "${splitme:0:33}\n${splitme:33}\n" > /tmp/newtext
cat < /tmp/newtext
exit 0

Results, (note cygwin therefore using dos2unix for this demo):-
Code:
AMIGA:~> cd /tmp
AMIGA:/tmp> chmod 755 split_me.sh
AMIGA:/tmp> dos2unix split_me.sh
dos2unix: converting file split_me.sh to Unix format ...
AMIGA:/tmp> ./split_me.sh
emplid|empl_name|salary|hire_date
1111|gro|3000|01/01/2014
9867|ghgo|4000|01/03/2014
5342|dkguo|5000|02/01/2014
7452|ehrt|6000|01/04/2014
2937|kfgtr|8000|03/01/2014
AMIGA:/tmp> _

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Split a line

I have a very long line in a file separated by "|" delimiter like below. Due to the length of the line, I find it very difficult to read to find a match line. file = temp.txt word 1| word 2 | word 3|.... I would like to read the file temp.txt and print out all words line by line like... (1 Reply)
Discussion started by: boldnbeautiful
1 Replies

3. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

4. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. UNIX for Dummies Questions & Answers

Using Awk to split a line

Hi, I have a file that contains multiple lines e.g. /Plane/Wing/Engine/Rotorblades I cannot use print $4 as the directories will be different lengths. All i would like to do is print the very last column in each line in the file i.e. in this case, Rotorblades. The code i... (4 Replies)
Discussion started by: crunchie
4 Replies

7. Shell Programming and Scripting

Split a line

I guess this has a simple solution but can't figure out now. having: x="H:a:b:c" to get H: echo $x|awk -F: {'print $1'} how can I put REST of line in another one? i.e. echo $rest a:b:c thanks ---------- Post updated at 08:58 PM ---------- Previous update was at... (5 Replies)
Discussion started by: garagonp
5 Replies

8. Shell Programming and Scripting

split single line into two line or three lines

Dear All, I want to split single line into two line or three lines wherever “|” separated values comes using Input line test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087, output shoule be test,DEMTEMPUT20100404010012,,,,,,,,0070086, test,DEMTEMPUT20100404010012,,,,,,,,0070087, (14 Replies)
Discussion started by: arvindng
14 Replies

9. Shell Programming and Scripting

Split a line on positions before reading complete line

Hi, I want to split before reading the complete line as the line is very big and its throwing out of memory. can you suggest. when i say #cat $inputFile | while read eachLine and use the eachLine to split its throwing out of memory as the line size is more than 10000000 characters. Can you... (1 Reply)
Discussion started by: vijaykrc
1 Replies

10. Shell Programming and Scripting

Split line before the pattern

Hi, I am trying to use awk to split line before the pattern: abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 I need it to be like this: abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde... (7 Replies)
Discussion started by: djanu
7 Replies
Login or Register to Ask a Question