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?
# 8  
Old 11-15-2013
Quote:
Originally Posted by pallvi_mahajan
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

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

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

# 9  
Old 11-15-2013
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.
Code:
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

# 10  
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"
To further explain what Akshay Hegde said, an awk program consists of pairs of the following form:
Code:
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:
Code:
{$f = w OFS $f}1

which is equivalent to:
Code:
{$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.
# 11  
Old 11-15-2013
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
Code:
echo "My test row" | awk '{$1=$2 OFS $1}1'


Last edited by Scrutinizer; 11-15-2013 at 09:56 PM.. Reason: code tags
# 12  
Old 11-16-2013
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.
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