Inserting a variable in awk script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting a variable in awk script
# 1  
Old 02-19-2016
Inserting a variable in awk script

I have file input.txt:
Code:
>TX1-1 Freq 55
cattctgatgaatatttgtcctttagttgttatttgt
>TX1-2 Freq 19
cattctgatgaatatttgtcctttagttgttatttgt
>TX1-3 Freq 17
cattctgatgaatatttgtcctttagttgttatttgt
>TX1-4 Freq 6
cattctgatgaatatttgtcctttagttgttatttgt
>TX1-5 Freq 6
cattctgatgaatatttgtcctttagttgttatttgt

And I am using the following ChangeID script to modify the ID (>XXXXX)
Code:
#!/bin/bash
if [ -f $1 ]
then
	file=$1
fi
pattern=$2
awk 'FNR==1{n=$pattern} /^>/ {print ">" n "-" ++c, $3;next}1' $file > New$file

So, when I enter ChangeID Input.txt Pat3, I am expecting to use the second field (Pat 3) to replace the beginning of the ID (>Pat3-XXX). Thus, the desire output file would look like this:
Code:
>Pat3-1 55
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-2 19
cattctgatgaatatttgtcctttagttgttatttgt
>Pat-3 17
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-4 6
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-5 6
cattctgatgaatatttgtcctttagttgttatttgt

But that did not work obviously. I have tried changing the AWK part of my script to
Code:
awk 'FNR==1{n=$2} /^>/ {print ">" n "-" ++c, $3;next}1' $file > New$file

But the script, of course, uses the second field on the row (Freq), outputting the following file:
Code:
>Freq-1 55
cattctgatgaatatttgtcctttagttgttatttgt
>Freq-2 19
cattctgatgaatatttgtcctttagttgttatttgt
>Freq-3 17
cattctgatgaatatttgtcctttagttgttatttgt
>Freq-4 6
cattctgatgaatatttgtcctttagttgttatttgt
>Freq-5 6
cattctgatgaatatttgtcctttagttgttatttgt

Any help will be greatly appreciated
# 2  
Old 02-20-2016
Making some guesses at what you were trying to do, you might want to consider this alternative script:
Code:
#!/bin/bash
if [ ! -r "$1" ]
then
	printf '%s: File not readable: "%s"\n' "${0##*/}" "$1" >&2
	exit 1
fi
pattern=${2:-Default}
awk -v n="$pattern" '/^>/ {print ">" n "-" ++c, $3;next}1' "$1" > "New$1"

With your sample data, invoking this script with:
Code:
./ChangeID input.txt Pat3

writes:
Code:
>Pat3-1 55
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-2 19
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-3 17
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-4 6
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-5 6
cattctgatgaatatttgtcctttagttgttatttgt

into Newinput.txt.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-20-2016
Hello Xterra,

If I understood your requirement correctly, then following may help you in same.
1st case: Let's say in case values in Input_file TX1-1 will be in increasing order then following may be helpful for same.
Code:
awk '/^>TX1/{sub("TX1","Pat3",$0);sub($2,X);print;next} 1'  Input_file

Output will be as follows.
Code:
>Pat3-1  55
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-2  19
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-3  17
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-4  6
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-5  6
cattctgatgaatatttgtcctttagttgttatttgt

2nd case: Assuming when your Input_file doesn't have values of >TX1 increasing order, then following may help you in same.
Code:
awk '/^>TX1/{sub("TX1","Pat3",$0);++c;sub(/\-.*[[:space:]]+/,"-" c" ",$0);print;next} 1'   Input_file

Output will be as follows.
Code:
>Pat3-1 55
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-2 19
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-3 17
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-4 6
cattctgatgaatatttgtcctttagttgttatttgt
>Pat3-5 6
cattctgatgaatatttgtcctttagttgttatttgt

Thanks,
R. Singh
# 4  
Old 02-20-2016
@Ravinder, with that approach no shell variables are passed to the awk script, which was what the OP was trying to achieve..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-20-2016
Thanks Scrutinizer Smilie, my bad I didn't understood it completely.

Hello Xterra,

Could you please with following scripts and let me know if this helps. It has conditions like to check Input_file which is being passed to script as variable if that is really present or not and then check the existence of $2 which is pattern we need to get in output. Though suggestion provided by Don is better than this.
For case 1st: When assuming your values are in increasing order for >TX1-.
Code:
cat script1.ksh
if [[ -f $1 ]]
then
        INPUT_FILE=$1
else
        echo "Input_file passed as an argument $1 is NOT found."
        exit;
fi
if [[ -n $2 ]]
then
        PATTERN=$2
else
        echo "You haven't entered the pattern so script is exiting now."
         exit;
fi
awk -vpattern="$2" '/^>TX1/{sub("TX1",pattern,$0);sub($2,X);print;next} 1' "$INPUT_FILE"

For case 2nd: When we are assuming your >TX1- is not in increasing order, it may help.
Code:
cat script1.ksh
if [[ -f $1 ]]
then
        INPUT_FILE=$1
else
        echo "Input_file passed as an argument $1 is NOT found."
        exit;
fi
if [[ -n $2 ]]
then
        PATTERN=$2
else
         echo "You haven't entered the pattern so script is exiting now."
          exit;
fi
 
awk -vpattern="$2" '/^>TX1/{sub("TX1",pattern,$0);++c;sub(/\-.*[[:space:]]+/,"-" c" ",$0);print;next} 1' "$INPUT_FILE"

Output will be as follows in both of the above cases.
Code:
./script1.ksh file3 Pat
>Pat-1 55
cattctgatgaatatttgtcctttagttgttatttgt
>Pat-2 19
cattctgatgaatatttgtcctttagttgttatttgt
>Pat-3 17
cattctgatgaatatttgtcctttagttgttatttgt
>Pat-4 6
cattctgatgaatatttgtcctttagttgttatttgt
>Pat-5 6
cattctgatgaatatttgtcctttagttgttatttgt

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-20-2016 at 04:45 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 02-20-2016
Thank you guys! Exactly what I needed
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Problem in inserting values of variable of shell

hi all, i have one shell script like this #!/bin/bash -xv ENV_NAME=`cat $IB_HOME_DIR/cfg/ibProfile.sh | grep "RDM_CONN" | cut -f 2 -d "@"` CURRENT_DIR=`pwd`; string=$IB_HOME_DIR string1="$string/FRGFOLDER/input" #sed "s/string3/$string1" frg_event_src.sql > modifiedinsert.sql sqlplus... (2 Replies)
Discussion started by: ramsavi
2 Replies

3. Shell Programming and Scripting

Inserting variable values in filename

Hi All, I have a directory containing multiple files. and also a txt file which contains the list of all filenames and certain values. I want to read the text file line by line and if its 2nd column is matched with the filename in directory, then it must insert the values in 7th column to... (14 Replies)
Discussion started by: CAch
14 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

6. Shell Programming and Scripting

inserting a variable to a new line

In my script I am using sed to insert a line. Suppose I have a variable called ck_size=5 and a temporary file called tmp I want to add a certain line in the tmp file .Please see the below command sed -i '2a\maitee is $ck_size' /dun/homes/lrsprod/tmp I want in 2 line of tmp file... (7 Replies)
Discussion started by: maitree
7 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

Inserting text to file, sed and variable not acting right

I want to put text stored in a variable into a file on the 7th line. I'm having trouble with this line: sed '7i\'$text'' $file It works perfectly for a single word, but fails if there are two words because of the space. I've tried several forms of quoting and this is the only one that... (2 Replies)
Discussion started by: fubaya
2 Replies

9. Shell Programming and Scripting

Inserting a line when its length is variable

Hi Unix experts I have simple text files in which the number of lines vary from one file to another. They look like the following: # # . . 34 46 76 72 39 68 I want to grab the first number of the last line of each file (let's say A= 39 in the above example), which is... (2 Replies)
Discussion started by: nxp
2 Replies

10. Shell Programming and Scripting

Inserting variable value into filename

Greetings, people of UNIX/Linux forums. I am having a problem with a script, where I am trying to create a new variable. The value of this variable would be dependent on the value in a couple other previous variables (all variables are 2-digit integers). Here is my code: #set the stations... (3 Replies)
Discussion started by: TheSMan5
3 Replies
Login or Register to Ask a Question