Trim and pad a field in the middle of unix file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trim and pad a field in the middle of unix file
# 1  
Old 12-07-2010
Trim and pad a field in the middle of unix file

Hello,

I have a file with several lines and I need to trim and pad with spaces the data that are between position 6 and 15 included. Data in position 1 to 5 and after 15 could be anything, and should stay as they are.

For instance, the following records in a file (underscore = space)
ABCDEF_G_H_I_J_KLMNO
12345_67_89_G_H_K_L

Should give
ABCDEFGHIJ_____KLMNO
123456789GH_____K_L

I am a real dummy in Unix, tried to play with awk and sed but this one is too hard for me Smilie! Any help would be really appreciated.

Thanks.

Last edited by BSF; 12-07-2010 at 11:18 AM..
# 2  
Old 12-07-2010
Looks like a sed looper problem, find a space+not-space between 6 and 15, and change it to a not-space space, and if found, try again. Looping sed is not so fast, but when you need tight control, you cannot do too much at once. It's a bit like a bubble sort:
Code:
sed '
  :loop
  s/^\(.\{5\}[^ ]\{0-8\}\) \([^ ]\)/\1\2 /
  t loop
 '

# 3  
Old 12-08-2010
Thanks! I managed to do it the following way using awk

awk '{str1=substr($0,1,5);str2=substr($0,6,10);str3=substr($0,16,length $0);
gsub(/ /,"",str2);gsub(/-/,"",str2);printf str1;printf("%-10s",str2);printf str3 "\n"}
# 4  
Old 12-08-2010
Looks good. FWIW, this awk does the same but it is a bit shorter..
Code:
awk '{s=t=substr($0,6,10); gsub(/[ -]/,x,t); sub(s,sprintf("%-10s",t))}1' infile


Last edited by Scrutinizer; 12-08-2010 at 11:59 AM..
# 5  
Old 12-08-2010
That one is nicer, I will keep it!
Thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

4. Shell Programming and Scripting

trim spaces in unix for variable

HI Guys I have written a script using awk to split a file based on some identifier and renaming the file based on two values from specific length. ts a fixed width file. When I am trying to fetch the values a = substr($0,11,10) b = substr($0,21,5); i am getting spaces in a and b values .... (6 Replies)
Discussion started by: manish8484
6 Replies

5. Shell Programming and Scripting

Pad zeroes first field in a Delimited file

Need help. I tried using an awk command to pad zeroes. Unfortunately, the "|" pipe delimited character is gone when I tried to write the records to another file. awk -F \| ' {$1=sprintf("%06s", $1); print $0}' $CUSTFINAL2 > $CUSTFINAL3 BEFORE "KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT... (3 Replies)
Discussion started by: johnhips
3 Replies

6. Shell Programming and Scripting

Unix File - Adding columns in the middle

Hello, I have a comma separated flat file. It contains some 20 columns. I want to add two new columns at position 2,3. So that file will have 22 columns. I am providing here sample data with file having 4 columns. Appreciate your help in finding solution for this. data in input file:... (11 Replies)
Discussion started by: ravi.videla
11 Replies

7. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

8. Shell Programming and Scripting

Trim leading zeros to make field 6 characters long

Hi all- I've got a file that will have multiple columns. In one column there will be a string that is 10 digits in length, but I need to trim the first four zeros to make it 6 characters? example: 0000001234 0000123456 0000234566 0000000321 output: 001234 123456 234566 000321 (5 Replies)
Discussion started by: Cailet
5 Replies

9. Shell Programming and Scripting

How to trim a 419th column in UNIX file.

Hi All, I have a UNIX file, which has Total of 532 column, each delimited with ^~^. One of the column (419) is 202 character long. In real case, it should be 22 character long. cut -d~ -f419 filename ... (4 Replies)
Discussion started by: Amit.Sagpariya
4 Replies

10. Shell Programming and Scripting

How to trim the leading zeroes in a Currency field ?

How do I trim the leading zeroes, and (+,-) in the currency field ? I have a text file. Your bill of +00002780.96 for a/c no. 25287324 is due on 11-06. Your bill of +00422270.48 for a/c no. 28931373 is due on 11-06. I want the O/P file to be like. Your bill of 2780.96 for a/c no. 25287324... (22 Replies)
Discussion started by: Amruta Pitkar
22 Replies
Login or Register to Ask a Question