Inserting n characters to beginning of line if match


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Inserting n characters to beginning of line if match
# 1  
Old 11-20-2017
Inserting n characters to beginning of line if match

I would like to insert n number of characters at the beginning of each line that starts with a given character. If possible, I would be most appreciative for a sed or awk solution.

Given the data below, I would like to be able to insert either 125 spaces or 125 "-" at the beginning of every line that begins with "@".

Code:
 
@PS001,001 VWB                 0   2 -1 -1 -1  5 -1   -1 -1  3  2     
@PS001,001 HJ==                0   7 -1 -1 -1 -1 -1   -1  3  1  2
 PS001,001 >CR=                0   2 -1 -1 -1  5 -1   -1 -1  3  2

Thank you so much.

Last edited by jvoot; 11-20-2017 at 06:30 PM..
# 2  
Old 11-20-2017
What have you tried so far?
This User Gave Thanks to Scott For This Post:
# 3  
Old 11-20-2017
I have tried various iterations of the following basic codes:

Code:
sed "/^@/s/^/i $(printf '%.0s-' {0..125}/g)" file1.txt

Code:
sed '/^@/s/^@/ \{125\}@/g' file1.txt

Code:
awk '{if($0 ~/^@/); BEGIN{for(c=0;c<125;c++) printf "-"; printf "\n"}' file1.txt

This User Gave Thanks to jvoot For This Post:
# 4  
Old 11-20-2017
Here are a couple of possible solutions:
Code:
echo "inserting 125 spaces..."
awk '{printf("%*s\n", 125 * (substr($0, 1, 1) == "@") + length, $0)}' file
echo "inserting -..."
awk '/^@/{$0 = "-" $0}1' file

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-20-2017
Thank you so much for this Don. In reviewing your code, I left things a bit ambiguous in my original post. I meant to say either 125 spaces or *125* dashes ("-"). I very much like the elegance of:

Code:
awk '/^@/{$0 = "-" $0}1' file

Is there a way that awk can be told to do 125 of the leading "-"? Or is the only solution to substitute "-" for "/s" in:

Code:
awk '{printf("%*s\n", 125 * (substr($0, 1, 1) == "@") + length, $0)}' file

Thus:
Code:
awk '{printf("%*-n", 125 * (substr($0, 1, 1) == "@") + length, $0)}' file

# 6  
Old 11-20-2017
Sorry for misunderstanding what you were trying to do. Maybe something like:
Code:
awk '
BEGIN {	for(i = 1; i <= 125; i++)
		dl = dl "-"
}
{	print (($0 ~ /^@/) ? dl : "") $0
}' file

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 11-21-2017
Try also
Code:
awk '1+gsub (/^@/, sprintf (" %125s", "@"))' file

and
Code:
awk 'BEGIN {T = sprintf ("%125s", " "); gsub (/ /,"-", T)} 1+gsub (/^@/, T "@")' file

or
Code:
awk 'BEGIN {while (length(T) < 125) T = T "-" } 1+gsub (/^@/, T "@")' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing characters from beginning of multiple files

Hi, I have been searching how to do this but I can't seem to find how to do it. Hopefully someone can help. I have multiplr files, 100's example 12345-zxys.213423.zyz.txt. I want to be able to take all these files and remove the first '12345-' from each of the files. '12345-' these characters... (5 Replies)
Discussion started by: israr75
5 Replies

2. Shell Programming and Scripting

[Solved] Inserting a line beginning with regex

I have been trying to insert a line after a regex but I can't do it. Here is the code I am using: cat saved_doc SET type = type1 SET type = STORE = y /vol/san_e1 /vol/san_5 /vol/san_e9 /vol/san_e11 /vol/san_e12 /vol/san_e13 /vol/san_e14 /vol/san_e16 /vol/san_e17 /vol/san_e18... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. UNIX for Dummies Questions & Answers

How to match on phrase at beginning of line and specific columns?

Here is my file: 700 7912345678910 61234567891234567891 700 8012345678910 61234567891234567891 I want to pull all lines that begin with '700' only if columns 11-12 are '79'. My code so far only pulls the '79', not the whole line: grep ^700 file1 | cut -c 11,12 |... (7 Replies)
Discussion started by: Scottie1954
7 Replies

4. Shell Programming and Scripting

Removing one or more blank characters from beginning of a line

Hi, I was trying to remove the blank from beginning of a line. when I try: sed 's/^ +//' filename it does not work but when I try sed 's/^ *//' filename it works But I think the first command should have also replaced any line with one or more blanks. Kindly help me in understanding... (5 Replies)
Discussion started by: babom
5 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

7. UNIX for Dummies Questions & Answers

Compare 2 lists using a full and/or partial match at beginning of line?

hello all, I wonder if anybody might be able to help with this. I have file 1 and file2. Both files may contain thousands of lines that have variable contents. file1 234GH 5234BTW 89er 678tfg 234 234YT tfg456 wert 78gt gh23444 (7 Replies)
Discussion started by: Garrred
7 Replies

8. UNIX for Dummies Questions & Answers

Inserting control characters at the end of each line

How to add control characters at the end of each line in a file? Can anyone help me with this? Thanks, Shobana (2 Replies)
Discussion started by: Shobana_s
2 Replies

9. Shell Programming and Scripting

inserting characters before each line...

Hi , the fog is fulling my brain after holidays , somebody can help me ? I have a file in input like that : toto tata tutu and trying with awk to insert the compete file string as : /dir1/dir2/toto /dir1/dir2/tata /dir1/dir2/tutu i used to write : awk 'BEGIN {FS="\\"} {print... (4 Replies)
Discussion started by: Nicol
4 Replies

10. Shell Programming and Scripting

Inserting new line after match of a fixed string

Hi, I have a file which contains many occurances of a string say "hellosunil". I want to insert a newline charcater after all the "hellosunil" strings in the file. trying to use sed, sed -e 's/hellosunil/\\nhellosunil/g' file1 sed help says u cannot substitute a regular expression... (6 Replies)
Discussion started by: sunil_neha
6 Replies
Login or Register to Ask a Question