Visit Our UNIX and Linux User Community


Append line based on fixed position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append line based on fixed position
# 1  
Old 03-19-2008
Append line based on fixed position

Hi all;

I'm having headache on append one line to another based on the fix position.Hope u guys can help.

All i need to do is append the line that start with '3' to a line which start with '1' and the position for line 3 that i need to append is 22.

The original file look like this:

1CSTN0012008010312
2A45021906080120070303131709000017600
2S45333282010520020065411701000017600
2Z30092614090520020065635811000026400
2Z21094701020220020065649305000019800
2S35343116010420020065650001000008800
2S35344198010120020065651208000006600
2S45333998010620020065702107000008800
2S35344254010220020065704708000013200
2D93688321010220020065708106000006600
2D93686199010420020065710405000009900
2K40052945050820020065714210000004400
2U30030159060220070304010210000013200
30000012000152900

I need to rearrange the file to make it look like this:

1CSTN0012008010312 30000012000152900
2A45021906080120070303131709000017600
2S45333282010520020065411701000017600
2Z30092614090520020065635811000026400
2Z21094701020220020065649305000019800
2S35343116010420020065650001000008800
2S35344198010120020065651208000006600
2S45333998010620020065702107000008800
2S35344254010220020065704708000013200
2D93688321010220020065708106000006600
2D93686199010420020065710405000009900
2K40052945050820020065714210000004400
2U30030159060220070304010210000013200

For your information the length for line that start with '1' is not fixed,it can be less than 21 character.

I'm having a problem when the length of the line (that starts with '0') is 21.
I try to use if ... else but got error msg. This is the script that i used

cat CSTN001.dat | awk '
/^1/ { line0 = $0}
{
if ( length(line0) < 21 )

FS=" "; for (i=1; i<(21-(length($0))); i++) FS=FS " ";
/^1/ { sub(/1/, "0") }
/^3/ { line1 = $0 }
/^0/ { $NF = ($NF FS line1) }
{ x[FNR] = $0 }
END{
for (i=1; i<=FNR; i++)
if (x[i] ~ /^0/)
print x[i]
for (j=1; j<=FNR; j++)
if (x[j] !~ /^[03]/)
print x[j]
}

else
/^1/ { sub(/1/, "0") }
/^3/ { line1 = $0 }
{ x[FNR] = $0 }
END{
for (i=1; i<=FNR; i++)
if (x[i] ~ /^0/)
print x[i] line1
for (j=1; j<=FNR; j++)
if (x[j] !~ /^[03]/)
print x[j]
}
}
' > CSTN001.new


Someone pls help...Thank you in advance
# 2  
Old 03-19-2008
Append line based on fixed position

awk '
/^1/ { s=$0 }
/^3/ {
if ( s == "" ) next
printf "%-21s%s\n",s,$0
s=""
}
' $INFILE

-Ramesh
# 3  
Old 03-19-2008
Thanks Ramesh

It works perfectly!!...really appreciate your help

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Append each line based upon the character size

I have a huge file which contains multiple lines. It need to check whether character length is not more than 255 each line. If its not then it should remove the character up to column. I have described in the output below. If its more than that the next line should start with call but if the... (1 Reply)
Discussion started by: JoshvaPeter
1 Replies

2. Shell Programming and Scripting

To append new data at the end of each line based on substring of last column

Hi guys, I need to append new data at the end of each line of the files. This new data is based on substring (3rd fields) of last column. Input file xxx.csv: U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0 U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0... (5 Replies)
Discussion started by: null7
5 Replies

3. Shell Programming and Scripting

Splitting based on occurence of a Character at fixed position

I have a requirement where i need to split a file based on occurence of a character which is present at a fixed position. Description is as below: 1. The file will be more than 1 Lakh records. 2. Each line will be of fixed length of 987 characters. 3. At position 28 in each line either 'C' or... (9 Replies)
Discussion started by: Neelkanth
9 Replies

4. Shell Programming and Scripting

Fixed width file search based on position value

Hi, I am unable to find the right option to extract the data in the fixed width file. sample data abcd1234xgyhsyshijfkfk hujk9876 io xgla loki8787eljuwoejroiweo dkfj9098 dja Search based on position 8-9="xg" and print the entire row output ... (4 Replies)
Discussion started by: onesuri
4 Replies

5. Shell Programming and Scripting

Print the complete line based on position as input

Hi , I have a text file like below abcd2223232321212121324343 asdsfffhfgfgfhgfhghghghghghgh dfdfdfgfghfgfgfgfgfgghghghghgh dfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf 1234567890sasasasfdfbffghffhf abcd222323232121212abcdsds in the above file i need to grep and print the lines if 14th to 18th... (4 Replies)
Discussion started by: aravindj80
4 Replies

6. Shell Programming and Scripting

Remove the spaces at the end of a line starting from a fixed position

I want to remove the trailing spaces at the end of each line starting from a particular position(using ksh script). For example, in the attached file, I want to remove all the spaces starting from the position 430 till the end. The space has to be removed only from the 430th position no matter in... (3 Replies)
Discussion started by: Suryaaravindh
3 Replies

7. Shell Programming and Scripting

Match data based on two fields, and append to a line

I need to write a program to do something like a 'vlookup' in excel. I want to match data from file2 based on two fields (where both match) in file1, and for matching lines, add the data from two of the fields from file2 to file1. If anyone knows something in perl or awk that can do this, I'd be... (20 Replies)
Discussion started by: jamessmith01
20 Replies

8. Shell Programming and Scripting

append each line on fixed position 31 to 33

I have a .DAT file like below. 26666666660001343 000001004OLF 029100020090820 27777777770000060 000001004ODL-CH001000020090820 28888888880000780 000001013OLF 006500020090820 ....... ........ and so on..... I want to append each line in a file in .KSH script with XXX with position... (5 Replies)
Discussion started by: kshuser
5 Replies

9. UNIX for Dummies Questions & Answers

Append line and variable at fixed postion in file in unix

Hi I have a input file ::: 1583904589034853904 1690234849023849023 159823890238409 1690238490238490238490 . . . The output file should have the record 16 appended to the record 15 and a variable should be added at a FIXED POSTION at 55. The records are been processed inside a loop... (3 Replies)
Discussion started by: akashhello
3 Replies

10. Shell Programming and Scripting

Spliting the line based on position.

i want to split a big line based on the position. example : I have a single line which has 2300 characters. i want to split from 1 character to 300th characters as first line and 301th to 600 as second line and 601th to 900 as third line ...till the end of the string. Can anyone help... (1 Reply)
Discussion started by: senthil_is
1 Replies

Featured Tech Videos