How to insert strings at certain position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert strings at certain position
# 8  
Old 11-14-2005
one way.....
Code:
nawk -v OFS=', ' '{ $1=$1; print sprintf("%-36s", $0) " 0000 00" }' myFile


Last edited by vgersh99; 11-14-2005 at 05:46 PM.. Reason: changed OFS to ', ' - added a trailing space
# 9  
Old 11-14-2005
OFS in awk

I am still getting "," as OFS.
So I changed OFS=', ' to OFS=' ' ,
But the same thing happend again.

1089, KEVIN_RIDDLE 0000 00
1137, STEPHEN_SMITH 0000 00
1214, MELISA_someone 0000 00
1267, SANDY_name 0000 00
# 10  
Old 11-14-2005
hmmmmm....... I thought that's what you have wanted!
Could you [as always] post the exact complete desired output, pls.
# 11  
Old 11-14-2005
Sorry.
OK. first one is what I got by running the code you provided:
1089, KEVIN_RIDDLE 0000 00

The one below is the one that I desired:

1089 KEVIN_RIDDLE 0000 00
# 12  
Old 11-14-2005
Quote:
Originally Posted by whatisthis
Sorry.
OK. first one is what I got by running the code you provided:
1089, KEVIN_RIDDLE 0000 00

The one below is the one that I desired:

1089 KEVIN_RIDDLE 0000 00
hmmm... maybe I'm getting "thick sculled', but....
didn't my original/first post attempt to do just that?
Code:
nawk '{ print sprintf("%-36s", $0), "0000 00" }' myFile


Last edited by vgersh99; 11-14-2005 at 06:17 PM..
# 13  
Old 11-14-2005
Data awk OFS

Oh, my god, I think I am too bad at communication.
Maybe that's why I am still just a poor programmer...

OK.
This is the file I originally had:
1089, KEVIN_RIDDLE
1137, STEPHEN_SMITH
1214, MELISA_someone
1267, SANDY_name

Two things I'd like to get from this file:
1. replace the comma with space for OFS
2. Add "0000 00" at each line of the original file according to the length of the name.

So this is going to be the final file I'd like to see:

1089 KEVIN_RIDDLE
0000 00
1137 STEPHEN_SMITH
0000 00
1214 MELISA_someone
0000 00
1267 SANDY_name
0000 00


The "0000 00" should be aligned from right side
# 14  
Old 11-14-2005
ok, now we're getting somewhere!
Code:
nawk -v FS=',' -v OFS=' ' '{ $1=$1; print sprintf("%-36s", $0), "0000 00" }' myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print strings from a particular position in each line

I am using bash in Fedora 30 From the below lines (ls -l output), how can I print whatever is between the strings 'status_' and '.log' $ ls -l | grep -i status -rw-rw-r--. 1 sysadmin sysadmin 378530 Nov 11 21:58 status_vsbm1.log -rw-rw-r--. 1 sysadmin sysadmin 428776 Nov 11 21:58... (8 Replies)
Discussion started by: kraljic
8 Replies

2. UNIX for Dummies Questions & Answers

Using sed to insert at position x and then every interval y for each line

Thanks to help from Don Cragun in post 302924174, I'm off and getting into trouble on my own (finally) with sed. Here is my goal - insert \\r\n at the 60th character on each line and then every 76th character thereafter: Input:... (3 Replies)
Discussion started by: gusbrown
3 Replies

3. Shell Programming and Scripting

How to insert a '#' in the first position of all the files?

Hi All, how to insert a '#' in the first position of all the files based on a certain condition. I tried this: cat /bin/user/input_file.txt | while read a do b=`sed 's/.*song=good.*/\#&/g' $a ` echo $b > /bin/user/new/output_file.txt done input_file.txt has list of names of 10... (5 Replies)
Discussion started by: anand787
5 Replies

4. Shell Programming and Scripting

Insert | in specific position

Hi , I have a file which has line similar to below 13123324234234234234234234234234234 3454546456dfhgfhgh454645654asdasfsdsddfgdgdfg 345345345mnmnbmnb346mnb4565464564564645645 Not for each line for specific position I need to insert some '|' Positions are fixed. Like 3,5,9,11 So the... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

5. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

6. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

7. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

8. Shell Programming and Scripting

insert server name in position 1 on each line

Hello, It has been a long time since I have written unix code and I need to insert a variable into the first position of each line in a file. Below is an example of the script and the desired output file Here is my short script server="$(hostname)" df -kg | awk '{print $1, $2, $3, $4,... (3 Replies)
Discussion started by: rlc198842
3 Replies

9. Shell Programming and Scripting

How to insert at a particular position in flat file

Hi All, I have a flat file with ~ as de-limiter (e.g: aaa~ba a~caa~0~d~e) What I want is check if the 4th character is 0 and replace it with say 4. So now it becomes : aaa~ba a~caa~4~d~e. I have to do this for the whole file, but the delimiter position remains the same, not the... (10 Replies)
Discussion started by: akdwivedi
10 Replies

10. HP-UX

find the position in a file and insert the data there

Hi, I have a EDI data file ARROWTEST of size 18246 characters. And I want to insert some data after 4200 position in the file. How I can find the position 4200 in that file....Please advise. Regards, (5 Replies)
Discussion started by: isingh786
5 Replies
Login or Register to Ask a Question