Appending string (charachters inside the line) to a fixed width file using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending string (charachters inside the line) to a fixed width file using awk or sed
# 1  
Old 12-08-2009
Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File:
Code:
abcdefghijklmnop01qrstuvwxyz
abcdefghijklmnop02qrstuvwxyz
abcdefghijklmnop03qrstuvwxyz
abcdefghijklmnop04qrstuvwxyz
abcdefghijklmnop05qrstuvwxyz

Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number value it shows

Output File:
Code:
01abcdefghijklmnop01qrstuvwxyz
a02bcdefghijklmnop02qrstuvwxyz
ab03cdefghijklmnop03qrstuvwxyz
abc04defghijklmnop03qrstuvwxyz
abcd05efghijklmnop05qrstuvwxyz


Last edited by zaxxon; 12-08-2009 at 03:14 AM.. Reason: code tags
# 2  
Old 12-08-2009
Code:
awk '
        {
                pat1=(substr($0,17,2))
                sub(pat1,"")
                pat2=substr($0,0,NR-1)
                sub(pat2,"")
                print pat2 pat1 $0
        }
' infile

Output:
Code:
01abcdefghijklmnopqrstuvwxyz
a02bcdefghijklmnopqrstuvwxyz
ab03cdefghijklmnopqrstuvwxyz
abc04defghijklmnopqrstuvwxyz
abcd05efghijklmnopqrstuvwxyz

# 3  
Old 12-08-2009
Wrench

Something like this?

Code:
awk '{$0=substr($0,1,i++) substr($0,17,2) substr($0,i)}1' file

# 4  
Old 12-08-2009
Debian

Thanks!!! worked like magic
# 5  
Old 12-08-2009
bash
Code:
while read -r line
do
    num=${line:16:2}
    echo "${line:0:$num-1}${num}${line:$num}"
done < "file"

# 6  
Old 12-08-2009
Code:
perl -ne '{s/(.*[^0-9]*)([0-9]+)([^0-9].*)/substr($1,0,$2-1).sprintf("%02d",$2).substr($1,$2-1).$2.$3/e;print;}'

# 7  
Old 12-09-2009
The OP asked:
Quote:
Originally Posted by tamahomekarasu
Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number value it shows
So imo only ghostdog and summer cherry's solutions are working correctly. I prefer Ghostdog's solution because it is nice and simple.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

2. Shell Programming and Scripting

Replace using awk on fixed width file.

All, I used to use following command to replace specific location in a fixed width file. Recently looks like my command stopped working as intended. We are on AIX unix. awk 'function repl(s,f,t,v) { return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) } NR<=10 {... (3 Replies)
Discussion started by: pinnacle
3 Replies

3. Shell Programming and Scripting

Print column details from fixed width file using awk command

hi, i have a fixed width file with multiple columns and need to print data using awk command. i use: awk -F "|" '($5 == BH) {print $1,$2,$3}' <non_AIM target>.txt for a delimiter file. but now i have a fixed width file like below: 7518 8269511BH 20141224951050N8262 11148 8269511BH... (5 Replies)
Discussion started by: kcdg859
5 Replies

4. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

5. Shell Programming and Scripting

awk: creating a fixed-width single file from 2 different files

I have to create a single file from three files, Please see below for samples: day.txt 20090101 20090102 item.txt 123456789101 12345678910209 1234567891 str.txt 1 12 123 output.txt 20090101123456789101 1 0 2009010112345678910209 12 ... (2 Replies)
Discussion started by: tamahomekarasu
2 Replies

6. Shell Programming and Scripting

Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake. I have had been able do the joins using sed, the thing I am... (10 Replies)
Discussion started by: Cho Nagurai
10 Replies

7. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

8. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

9. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies

10. UNIX for Dummies Questions & Answers

Fixed Width file using AWK

I am using the following command at the Unix prompt to make my 'infile' into a fixed width file of 100 characters. awk '{printf "%-100s\n",$0}' infile > outfile However, there are some records with a special character "©" These records are using 3 characters in place of one and my record... (2 Replies)
Discussion started by: alok.benjwal
2 Replies
Login or Register to Ask a Question