Add character to specific columns using sed or awk and make it a permanent change


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add character to specific columns using sed or awk and make it a permanent change
# 1  
Add character to specific columns using sed or awk and make it a permanent change

Hi,

I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches.

file has lot of functions defined
a.sh
Code:
#!/bin/bash

 fn a {
beautiful evening
sunny day
}
fn b {
}

fn c {
 hello world .its a beautiful day
 my mom is beautiful.
 my doll is beautiful
 all things bright and beautiful
 all creatures great and small
}

so when i execute the command on a.sh, the file should only be modified for places that has beautiful word

so the new file should be
a.sh
Code:
#!/bin/bash

 fn a {
# beautiful evening
sunny day
}
fn b {

}

fn c {
# hello world .its a beautiful day
# my mom is beautiful.
# my doll is beautiful
# all things bright and beautiful
 all creatures great and small
}

command i could create was
Code:
cat a.sh | grep -i "beautiful" | awk -F" " '{$1="#" $1;}1' OFS=" "

but it doesnt modify in the file. It only gives op on comd prompt. I dont want to create a new file. I want it modified in original file

Last edited by Don Cragun; 10-20-2015 at 11:03 AM.. Reason: Add CODE tags.
# 2  
Please use code tags as required by forum rules!

Try
Code:
awk '/beautiful/ {printf "#"} 1' file > tmp$$; mv tmp$$ file

search these fora for "in place file replacement".
This User Gave Thanks to RudiC For This Post:
# 3  
Sorry new to unix.com. Didnt explore much.
Thanks!!! It resolved the issue.
Can you explain temp$$?
# 4  
Hello ashima,

You are most welcome to forum, hope you will enjoy learning here. echo $$ will be used to write PID of the current shell.
So tmp$$ is just a Temporary file created in above code, which is later renamed to actual file. Complete explanation is as follows.
Code:
 
awk '/beautiful/  #### Searching for string beautiful here.
{printf "#"}      #### If above condition is TRUE then printing string #
1'                #### awk works on pattern of condition and action/operation so if we give condition here 1 which means we are making condition TRUE so default action will be happening here which is print for awk.
file > tmp$$;     #### mentioning Input_file named file here as putting output into file named tmp with $$ shell's pid.
mv tmp$$ file     #### renaming tmp$$ file to actual Input_file which is file.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
You could also try ed or ex with any shell that has a syntax based on the Bourne shell (e.g., ash, bash, ksh, zsh...):
Code:
#!/bin/ksh
ed -s a.sh <<-EOF
	g/beautiful/s/^/#/
	w
	q
EOF

These utilities might use a temp file internally, but you won't need to create one on your own this way.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Code:
sed -n 'p; /{/{ :L n; /}/p; s/\(.*\)/#\1/p; bL; };n;p;' file6
#!/bin/bash

 fn a {
# beautiful evening
# added
# sunny day
 }
# }
# fn b {
 }
# }
#
# fn c {
#  hello world .its a beautiful day
#  my mom is beautiful.
#  my doll is beautiful
#  all things bright and beautiful
#  all creatures great and small
 }
# }

I have tried branching
Code:
/}/p;

with
Code:
{/}/{p; bL;}}

I just get an error. I have branched successfully with L but can't seem to be able inside another Curly set.
I know I don't yet have the correct output for # on last line of curly set, right now I just want to understand the branching stuff, then I will try to fix the other problem after.
Thanks
# 7  
Not sure what you want to achieve. Your script prefixes almost - not all - lines with a # , except when an opening brace is encoutered. That could be done more easily with e.g. sed '/{/ !s/^/#/' file

n;p; doesn't mean a thing; if you drop it, sed will automatically read a new line and print it.
You don't match the pattern (beautiful) which is required.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #31
Difficulty: Easy
Alan Turing was the founder of IBM.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add specific text to columns in file by sed

What is the proper syntax to add specific text to a column in a file? Both the input and output below are tab-delineated. What if there are multiple text/fields, such as /CP&/2 /CM&/3 /AA&/4 Thank you :). sed 's/*/Index&/1' del.txt.hg19_multianno.txt > matrix.del.txt (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

3. UNIX for Dummies Questions & Answers

Will authconfig make permanent change or lost after reboot?

Hi, I made following configuration to create user directory: # authconfig --enablemkhomedir --update But the directory is created as permission 755, I'd like to modify the script to change directory access permission to 700, where is the script which copies /etc/skel to /home... (0 Replies)
Discussion started by: hce
0 Replies

4. Shell Programming and Scripting

change character(s) in specific column

Hi all! I need to change the final e every time when it is present in any word in column 1 to a; moreover, to change the final i again to a in any word in column 1, but just if word in column 2 begins with ha or si. Here below you can see a sample of my data: achwa ungeliachwa ungeli 1... (3 Replies)
Discussion started by: mjomba
3 Replies

5. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

6. Shell Programming and Scripting

AWK or SED to add string at specific position

Greetings. I don't have experience programing scripts. I need to insert a string in a specific position of another string on another file (last.cfg), for example: File last.cfg before using script: login_interval=1800 lcs.machinename=client04 File last.cfg after using script:... (4 Replies)
Discussion started by: vanesuke
4 Replies

7. Emergency UNIX and Linux Support

awk- add columns and make new column and save as newfile

Hi, I have file as below: 5 6 7 4 8 9 3 5 6 output needs to be another file with 4th column as $1+$2 and 5th column as $3+$4. sample output file 5 6 7 11 18 4 8 9 12 21 3 5 6 8 14 Anybody have answer Thanks in advance (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

8. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

9. Shell Programming and Scripting

How to change a specific character in a file

Hi, I have a data file with following structure: a|b|c|d|3|f1|f2|f3 a|b|c|d|5|f1|f2|f3|f4|f5 I want to change this data to: a|b|c|d|3|f1;f2;f3 a|b|c|d|5|f1;f2;f3;f4;f5 Data in column 5 tells the number of following fields. All fields delimiter after the 5th column needs to be... (6 Replies)
Discussion started by: sdubey
6 Replies

10. UNIX for Dummies Questions & Answers

How to make ulimit change permanent

ulimit -a gives the following output:$ulimit -a time(seconds) unlimited file(blocks) 2097152 data(kbytes) 131072 stack(kbytes) 16384 memory(kbytes) unlimited coredump(blocks) 32768 nofiles(descriptors) 400 vmemory(kbytes) 147456 Abot output... (3 Replies)
Discussion started by: nervous
3 Replies