Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-02-2009
Registered User
 

Join Date: Nov 2009
Posts: 58
Thanks: 7
Thanked 0 Times in 0 Posts
Add characters at specific position in file

Hello

I want to add some value at the specific position.
My file has data like

Hello
Welcome to UNIX Forums
Need Assistance

I want to add some value at the end but at same character position for all lines.

I want my output file to have data like :

Here '_' represents blanks.

Hello_____________________Everyone

Welcome to UNIX Forums____Everyone

Need Assistance___________Everyone

The data to be appended at last is same.

I am using this command :

Code:
 
sed "s/$/'Everyone'/" myfile

However it is appending where my line is ending.i.e.

Hello'Everyone'
Welcome to UNIX Forums'Everyone'
Need Assistance'Everyone'

Any suggestions.[COLOR="#738fbf"]
Sponsored Links
    #2  
Old 12-02-2009
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 7,287
Thanks: 55
Thanked 427 Times in 408 Posts
Something like this:


Code:
awk '{len=30-length;printf("%s%*s\n",$0,len,"Everyone")}' file

Sponsored Links
    #3  
Old 12-02-2009
frans's Avatar
frans frans is offline Forum Advisor  
Registered User
 

Join Date: Oct 2009
Location: France
Posts: 837
Thanks: 4
Thanked 52 Times in 51 Posts
You can use "printf" to format the input to the specified length. See the printf manual for complete syntax.
    #4  
Old 12-02-2009
Registered User
 

Join Date: Nov 2009
Posts: 58
Thanks: 7
Thanked 0 Times in 0 Posts
Thanks for valuable reply...

I am using this command .


Code:
 
awk '{len=4000-length;printf("%s%*s\n",$0,len,$var1$var2)}' $FILE

However I am getting an error :


Code:
awk: Field is not correct .The input line number is 1.The file is /.../abc.txt . 
The Source line number is 1.

Please help me in correcting this error.

---------- Post updated at 04:56 PM ---------- Previous update was at 04:21 PM ----------

THANK YOU EVERY ONE !!!

UNIX FORUMS ROCKK !!!

I GOT THE SOLUTIONS.

I LEARNED THAT I NEED TO PUT MY VARIABLES IN "'" QUOTES INSIDE AWK...

CHEERS !!!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert character in a specific position of a file gpaulose Shell Programming and Scripting 1 10-07-2009 12:27 PM
Print lines with specific character at nth position in a file manaswinig Shell Programming and Scripting 1 06-04-2009 09:01 AM
Print lines with specific character at nth position in a file manaswinig Shell Programming and Scripting 2 06-04-2009 09:00 AM
Deleting Characters at specific position in a line if the line is certain length Cailet Shell Programming and Scripting 10 12-17-2008 01:41 PM
read space filled file and replace text at specific position COD Shell Programming and Scripting 6 04-21-2008 05:40 AM



All times are GMT -4. The time now is 12:47 AM.