How to insert a word into a text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert a word into a text file?
# 1  
Old 11-14-2013
How to insert a word into a text file?

Hi,

I have a text file with one line having few words separated by space and I need to insert another word on "n"th column/field so that previous word should shift right (to n+1st column). how can I do that?

It seems we can do using awk but unable to figure out.

Please advise, thanks!

Code:
#cat file1
aaa bbb ccc ddd

Code:
#script.sh file1 new-word 2  (  2 indicates column number)
#cat file1
aaa new-word bbb ccc ddd

# 2  
Old 11-14-2013
You could set script.sh to something like:
Code:
awk -v w="$2" -v f="$3" '{$f = w OFS $f}1' "$1"

but, of course, you should add some error checking to be sure that all of the operands are present, the 1st is the name of an existing file, and that the last one is a numeric string.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 3  
Old 11-14-2013
Below will replace the file in-place as requested. Note this is usually not recommended and most unix utilities require you to output to new file.

Code:
trap 'rm -f /tmp/$$_newcol; exit' EXIT 1 2 3 15
awk -v w="$2" -v f="$3" '{$f = w OFS $f}1' "$1" > /tmp/$$_newcol && mv /tmp/$$_newcol "$1"

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 11-15-2013
How about the good ole and trusty ex...
Code:
ex -sc 's/aaa/& new-word/ | wq' file1

# 5  
Old 11-15-2013
hi shamrock

what is purpose of 1here which is in red color
Code:
awk -v w="$2" -v f="$3" '{$f = w OFS $f}1'"$1"

# 6  
Old 11-15-2013
Quote:
Originally Posted by pallvi_mahajan
hi shamrock

what is purpose of 1here which is in red color
awk -v w="$2" -v f="$3" '{$f = w OFS $f}1' "$1"
Instead of writing print 1 is used there, if you remove 1 you won't be able to see any output
# 7  
Old 11-15-2013
so we can use 1 in place of print in awk or it is specified one
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert text within a file?

Hi, I am trying to check for missing dates in a file and would want to insert the missing date into the file. Currently the script is as below #!/bin/ksh dates="dates" cat ${dates} | grep -v "^#" curr_month=`date '+%m` curr_day=`date '+%d` curr_year=`date '+%Y` #curr_month=02... (7 Replies)
Discussion started by: newbie_01
7 Replies

2. Shell Programming and Scripting

Insert value to db from text file

Hi, I have a single value in insertval file. I want to load that value to database with the current date. I tried the below code but it is inserting <NULL> to database and echo $c is also null. cat insertval | awk -F ' ' '{print $1}' > c echo c=$c data=`sqlplus -s user/pwd@hostname <<EOF ... (5 Replies)
Discussion started by: Neethu
5 Replies

3. Shell Programming and Scripting

insert text into empty file

I have an awk script to extract data from several files and create output in the following format as a csv file: xxxx 01/04/12 0001 0 When data is present, I have a file. When no data is available in the input files, I would still like to create a file that looks like this: xxxx... (1 Reply)
Discussion started by: banjo25
1 Replies

4. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

5. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

6. Shell Programming and Scripting

Insert Text On file

Hi All, Can someone pls help me to insert some text on a file. my file contains something like below.. AKBULBU, BALUMIL, BATCH,BATCH BOARROB, BOTAKAT, C57896, CAKIOZE, CHECMER, CICOFRA, CISZPAW,2194485 I want output as USER_ID, LOGIN_ID (6 Replies)
Discussion started by: harshakusam
6 Replies

7. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

10. Shell Programming and Scripting

insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file. i tried with sed but didn't managed yet. Is there another command or this one(sed) works? 10x anyway. (7 Replies)
Discussion started by: atticus
7 Replies
Login or Register to Ask a Question