Concatenating awk fields with variable

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating awk fields with variable
# 1  
Old 09-09-2013
Concatenating awk fields with variable


I am trying to do this:-

FILE=`echo $FILE | awk -F "." '{print $1 "$PID" $2}'`

echo $FILE

I need the output to be application12345.log but I am not sure how to get the $PID variable into the output. I have tried various things but not getting very far!

Any help appreciated!

Many thanks

Last edited by vbe; 09-09-2013 at 06:32 AM..
# 2  
Old 09-09-2013
Dear Simon.

Try below one, stop using grave accents it's not good way

akshay@Lenovo-E49:~$ FILE=application.log
akshay@Lenovo-E49:~$ PID=12345
akshay@Lenovo-E49:~$ FILE=$(echo $FILE | awk -F"." -v t=$PID '{print $1t"." $2}')
akshay@Lenovo-E49:~$ echo $FILE

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 09-09-2013
Perfect, thanks!
# 4  
Old 09-09-2013
No need for awk. Use your shell's parameter expansion opportunities:
echo ${FILE%.*}$PID.${FILE#*.}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: subset of fields as variable with sprint

Dear Unix Gurus, input: A|1|2|3|4|5 B|1|2|3|4|3 C|1|2|3|4|1 D|1|9|3|4|12 output: A_(5);B(3);C(1)|1|2|3|4 D_(12)|1|9|3|4 Details: If $2, $3, $3, $5 are identical, concatenate $1 and associated $NF together in the first field. But I am trying to do the above by passing the identical... (6 Replies)
Discussion started by: beca123456
6 Replies

2. Shell Programming and Scripting

Concatenating(??) shell variable with string

Dear Users, I am writing a for loop in shell (BASH), and am struggling with a very specific detail: SU_DATA=/home/........./su/ for(blah blah blah) { a=1001 b=1002 suop2 $SU_DATA/$ $SU_DATA/$ op=sum > } Here, it looks like the variables are... (1 Reply)
Discussion started by: martm
1 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

awk processing of variable number of fields data file

Hy! I need to post-process some data files which have variable (and periodic) number of fields. For example, I need to square (data -> data*data) the folowing data file: -5.34281E-28 -3.69822E-29 8.19128E-29 9.55444E-29 8.16494E-29 6.23125E-29 4.42106E-29 2.94592E-29 1.84841E-29 ... (5 Replies)
Discussion started by: radudownload
5 Replies

5. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

8. Shell Programming and Scripting

Concatenating lines ending with '+' using awk

Hi, I have an ASCII text file where some of the lines are ending with '+' character. I have to concatenate the next successive line with those lines having the trailing '+' char by removing that char. The below awk code has some problems to do this task: awk '{while(sub(/\+$/,"")) {... (12 Replies)
Discussion started by: royalibrahim
12 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. UNIX for Dummies Questions & Answers

concatenating string and variable

how to concatenate a string and variable like a=rahul and i want to put it into another variable 'b' as "rahul_prasath" i dont want to use another variable for "_prasath" how to do it? (1 Reply)
Discussion started by:
1 Replies
Login or Register to Ask a Question