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:
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:
which is equivalent to:
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)