Sponsored Content
Top Forums Shell Programming and Scripting Append line based on fixed position Post 302176677 by ashikin_8119 on Tuesday 18th of March 2008 10:47:30 PM
Old 03-18-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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
XINIT(1)                                                      General Commands Manual                                                     XINIT(1)

NAME
xinit - X Window System initializer SYNOPSIS
xinit [ [ client ] options ... ] [ -- [ server ] [ display ] options ... ] DESCRIPTION
The xinit program is used to start the X Window System server and a first client program on systems that are not using a display manager such as xdm(1) or in environments that use multiple window systems. When this first client exits, xinit will kill the X server and then terminate. If no specific client program is given on the command line, xinit will look for a file in the user's home directory called .xinitrc to run as a shell script to start up client programs. If no such file exists, xinit will use the following as a default: xterm -geometry +1+1 -n login -display :0 If no specific server program is given on the command line, xinit will look for a file in the user's home directory called .xserverrc to run as a shell script to start up the server. If no such file exists, xinit will use the following as a default: X :0 Note that this assumes that there is a program named X in the current search path. The site administrator should, therefore, make a link to the appropriate type of server on the machine, or create a shell script that runs xinit with the appropriate server. Note, when using a .xserverrc script be sure to ``exec'' the real X server. Failing to do this can make the X server slow to start and exit. For example: exec Xdisplaytype An important point is that programs which are run by .xinitrc should be run in the background if they do not exit right away, so that they don't prevent other programs from starting up. However, the last long-lived program started (usually a window manager or terminal emula- tor) should be left in the foreground so that the script won't exit (which indicates that the user is done and that xinit should exit). An alternate client and/or server may be specified on the command line. The desired client program and its arguments should be given as the first command line arguments to xinit. To specify a particular server command line, append a double dash (--) to the xinit command line (after any client and arguments) followed by the desired server command. Both the client program name and the server program name must begin with a slash (/) or a period (.). Otherwise, they are treated as an arguments to be appended to their respective startup lines. This makes it possible to add arguments (for example, foreground and back- ground colors) without having to retype the whole command line. If an explicit server name is not given and the first argument following the double dash (--) is a colon followed by a digit, xinit will use that number as the display number instead of zero. All remaining arguments are appended to the server command line. EXAMPLES
Below are several examples of how command line arguments in xinit are used. xinit This will start up a server named X and run the user's .xinitrc, if it exists, or else start an xterm. xinit -- /usr/local/bin/Xvnc :1 This is how one could start a specific type of server on an alternate display. xinit -geometry =80x65+10+10 -fn 8x13 -j -fg white -bg navy This will start up a server named X, and will append the given arguments to the default xterm command. It will ignore .xinitrc. xinit -e widgets -- ./Xorg -l -c This will use the command .Xorg -l -c to start the server and will append the arguments -e widgets to the default xterm command. xinit /usr/ucb/rsh fasthost cpupig -display ws:1 -- :1 -a 2 -t 5 This will start a server named X on display 1 with the arguments -a 2 -t 5. It will then start a remote shell on the machine fasthost in which it will run the command cpupig, telling it to display back on the local workstation. Below is a sample .xinitrc that starts a clock, several terminals, and leaves the window manager running as the ``last'' application. Assuming that the window manager has been configured properly, the user then chooses the ``Exit'' menu item to shut down X. xrdb -load $HOME/.Xresources xsetroot -solid gray & xclock -g 50x50-0+0 -bw 0 & xload -g 50x50-50+0 -bw 0 & xterm -g 80x24+0+0 & xterm -g 80x24+0-0 & twm Sites that want to create a common startup environment could simply create a default .xinitrc that references a site-wide startup file: #!/bin/sh . /usr/local/lib/site.xinitrc Another approach is to write a script that starts xinit with a specific shell script. Such scripts are usually named x11, xstart, or startx and are a convenient way to provide a simple interface for novice users: #!/bin/sh xinit /usr/local/lib/site.xinitrc -- /usr/bin/X -br ENVIRONMENT VARIABLES
DISPLAY This variable gets set to the name of the display to which clients should connect. XINITRC This variable specifies an init file containing shell commands to start up the initial windows. By default, .xinitrc in the home directory will be used. FILES
.xinitrc default client script xterm client to run if .xinitrc does not exist .xserverrc default server script X server to run if .xserverrc does not exist SEE ALSO
X(7), startx(1), Xserver(1), Xorg(1), xorg.conf(5), xterm(1) AUTHOR
Bob Scheifler, MIT Laboratory for Computer Science X Version 11 xinit 1.3.2 XINIT(1)
All times are GMT -4. The time now is 04:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy