Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add 20 spaces on specific line 1

Status
Not open for further replies.

dbase77

Technical User
Apr 23, 2002
591
IE
Hi All,

I have a file where first 4 lines and the last line are 80 characters. The rest are on 100 characters in each line. I want all the lines are 100 characters. So, is there any way to add 20 spaces on line 1-4 and last line? If there is a better way to do this, that would be great.

Thanks.
 
Hi

Code:
[gray]# if you are sure about the line numbers and their size[/gray]
sed '1,4s/$/[green]hit 20 spaces here[/green]/;$s/$/[green]hit 20 spaces here[/green]/' /input/file > /output/file

[gray]# for any line if shorter than 100 characters[/gray]
while read s; do test ${#s} -lt 100 && printf "%-100s\n" "$s" || echo "$s"; done < /input/file > /output/file

[gray]# or the same with [tt]awk[/tt][/gray]
awk 'length($0)<100{$0=sprintf("%-100s",$0)}1' /input/file > /output/file
Tested with GNU [tt]sed[/tt], [tt]gawk[/tt], [tt]bash[/tt] and (pd)[tt]ksh[/tt].

Feherke.
 
Hi,

It worked as expected. Thank you so much for the solution.
 
Here's another sed method:
Code:
sed -e :a -e 's/^.\{1,100\}$/& /;ta'

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top