Prefix/Suffix on same file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Prefix/Suffix on same file
# 1  
Old 05-04-2015
Bug Prefix/Suffix on same file

Hi,
I want to add prefix and suffix on line# 205 using SED or AWK and want to change on the same file without creating new file.

This command will be used in the bash script

Am using Bash shell

Regards
Nayaj
# 2  
Old 05-04-2015
sed and awk are stream editors. they can read files, but they don't really write them. some sed's have a -i option for in place editing, but it's somewhat broken and isn't portable.
you can use either and write to a new file, and then if that works, move it over the intended file.

Code:
awk 'NR==205 { print "prefix" $0 "suffix"; };1' file > newfile.$$ && mv newfile.$$ file

or use ed, which is a standard line-oriented file editor
Code:
printf '%s\n' '205s/.*/prefix&suffix/' w | ed -s file
    # or as a here-doc:
ed -s file <<!
205s/.*/prefix&suffix/
w
!

# 3  
Old 05-05-2015
Thanks for the quick reply.

I added below statement in my script and getting error.

Code:
 awk 'NR==$(($last_line_num - 6)) {print "<b>" $0 "</b>"; };1' report_out.txt > new_report && mv new_report report_out.txt

Error line:
Code:
awk NR=='$(($last_line_num - 6)) {print "<b>" $0 "</b>"; };1' report_out.txt

Error:
Code:
 awk: (FILENAME=/mypath/report_out.txt FNR=1) fatal: attempt to access field -6


Last edited by Don Cragun; 05-06-2015 at 03:43 AM.. Reason: Add CODE tags.
# 4  
Old 05-05-2015
you can't pass a shell variable into an awk program like that.

Personally I'd go with the ed. A here-doc does expand shell variables.

Code:
ed -s report_out.txt <<!
$((last_line_num - 6))s#.*#<b>&</b>#
w
!

or in bash as a here-string: ed -s report_out.txt <<< $'$((last_line_num-6))s#.*#<b>&</b>#\nw'

or with printf: printf %s\\n "$((last_line_num-6))s#.*#<b>&</b>#" w | ed -s report_out.txt

or fixing the awk solution:
Code:
awk -v line="$((last_line_num - 6))" 'NR==line {print "<b>" $0 "</b>"; next; };1' report_out.txt > new_report && mv new_report report_out.txt

This User Gave Thanks to neutronscott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

2. Shell Programming and Scripting

Lftp sftp get - script renames the local file with suffix tilde

Hi, Below script used for sftp get, #/bin/bash USER=xxx PASS=xxx HOST=xxx REMOTE_FILE=$1 LOCAL_FILE_LOC=$2 cd $LOCAL_FILE_LOC lftp sftp://$USER:$PASS@$HOST:10022 -e "get $REMOTE_FILE; bye" If file does not exist in sftp server, and file (same as remote file name) exists in local dir,... (4 Replies)
Discussion started by: vhegde1011
4 Replies

3. Shell Programming and Scripting

AWK adding prefix/suffix to list of strings

75 103 131 133 138 183 197 221 232 234 248 256 286 342 368 389 463 499 524 538 (5 Replies)
Discussion started by: chrisjorg
5 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. UNIX for Dummies Questions & Answers

Help deleting prefix from file names

I have a directory full of personal files named similar to the following: 043 fflshju 923- osfjpojfsa 1 - oasfns.txt:wall: What I would like to do is strip any leading numbers, spaces, dashes (etc.).. and leave only the main section of the name. For example, the preceeding list would be... (1 Reply)
Discussion started by: lazerking9
1 Replies

6. Shell Programming and Scripting

Replace prefix and suffix of a string

Hi, I'm new about shell scripting, and I need to do something like abcd **1234** efgh by abcd '''1234''' efgh I know that command sed helps about change one string by another, but I dont know how to keep whatever is inside **_** and replace * with '. Thanks! (5 Replies)
Discussion started by: selvaya
5 Replies

7. Shell Programming and Scripting

Remove prefix per line in file

Hi, I'm using a .ksh script to split one file into multible files by checking for the prefix per line. It works perfekt (thanks again for anyone involved in helping me with that ;)), but I want to remove the prefix per line too. Means only the line information itself should remain in the... (7 Replies)
Discussion started by: spidermike
7 Replies

8. Shell Programming and Scripting

File name suffix with a running number

Hi all, i have a shell script invoking a pl/sql proc which then, generates a file in a format DATA-26-12-2009.txt. The shell script is configured as a cron job to run once a day. If i need to run the script more than once, i need to suffix the generated file with a two digit running... (4 Replies)
Discussion started by: krishna.saran
4 Replies

9. Shell Programming and Scripting

prefix suffix to each argument

Hi, I have a variable, which contains comma separated values. Something like. StringA="abc,def,ghi,jkl" I want to apply prefix and suffix to each value in the string without using any loops. Say if Prefix is Pre_ and Suffix is _Suf then I need to get ... (1 Reply)
Discussion started by: tostay2003
1 Replies

10. Shell Programming and Scripting

Check if a file exists with certain prefix

Hi guys, How would i check a file exists with certainprefix? i have a directory with some files: ABC1 ABC2 ABC3 etc.. and want to do: please note i am using the korn shell environment.As when i gone through some stuff on then net i came to know some of the options will work... (11 Replies)
Discussion started by: raoscb
11 Replies
Login or Register to Ask a Question