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!

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

You could set script.sh to something like:

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 .

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.

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"
1 Like

How about the good ole and trusty ex...

ex -sc 's/aaa/& new-word/ | wq' file1

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

so we can use 1 in place of print in awk or it is specified one

Look at the difference here, we use usually to make code simple

$ echo "My test row" | awk '{$1=$2 OFS $1}1'
test My test row
$ echo "My test row" | awk '{$1=$2 OFS $1;print}'
test My test row

I guess using '1' makes the program more cryptic rather than making it simple. To conserve space, programmers normally use '1' instead of print.

In awk the default action if a condition is true is to print record.

So you can use any number other than 0 to print record, this is because 0 represents false and non-zero represents true.

awk 1 filename # will print records in file
awk 5 filename # will print records in file
awk 0 filename # will not print records in file

[/COLOR]

To further explain what Akshay Hegde said, an awk program consists of pairs of the following form:

pattern { action }

If the given pattern evaluates to a non-zero value for the current input line, the statements included in the action are executed for that line. If pattern is missing, the statements in action will be performed for every input line.
If a pattern is specified and there is no { action } , the action defaults to print (which is shorthand for print $0 ) which prints the current line.

So, the program:

{$f = w OFS $f}1

which is equivalent to:

{$f = w OFS $f}
1

contains two pairs. The first has no pattern and sets the field named by f to the given word followed by the output field separator followed by the original contents of that field. The second pair has no action and since 1 is non-zero, prints the (modified) current line.

thanks everyone

---------- Post updated at 03:57 PM ---------- Previous update was at 03:54 PM ----------

one more query; like if we need to mentioned print then we need to mentioned inside the bracket and if we need to use 1 the out of bracket is it

echo "My test row" | awk '{$1=$2 OFS $1}1'

The 1 here replace
1 {print $0}
You can also remove the 1 since no pattern gives true and just do:
{print $0}
or
{print}

and since expression before also does not have any pattern, just the code {code} , we can put the print there too since its always true.
{$1=$2 OFS $1;print}

Do study some tutorial for awk and you will see how it works.