Computing.Net > Forums > Unix > Adding variable at end of the row

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Adding variable at end of the row

Reply to Message Icon

Name: nimi
Date: July 4, 2006 at 20:16:36 Pacific
OS: SUN SOLARIS
CPU/Ram: NA
Product: SUN
Comment:

Hi,

How do I append the filename to the end of every rows in a text file?

my filename is ABC

exiting lines

CUPD|0|20205|FALSE|FALSE|International
CUPD|1|20206|FALSE|FALSE|International
CUPD|2|20207|FALSE|FALSE|International

i want to add ABC to the end of every rows.

CUPD|0|20205|FALSE|FALSE|International|ABC
CUPD|1|20206|FALSE|FALSE|International|ABC
CUPD|2|20207|FALSE|FALSE|International|ABC

Thanks

Nimi



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: July 4, 2006 at 21:42:53 Pacific
Reply:

Here are two ways. Using awk:

awk ' {
print $0"|"FILENAME
} ' ABC

# and sed:

VAR="ABC"
sed 's/$/|'"${VAR}"'/' ABC


0

Response Number 2
Name: nimi
Date: July 4, 2006 at 23:27:15 Pacific
Reply:

Hi Thanks Nails..

I used the following command
VAR="MSS"
sed 's/$/|'"${VAR}"'/' ABC.txt > test1.txt
mv test1.txt ABC.txt

i got what i wanted.

how do u use awk for the same scenario?

VAR="MSS"
awk ' {print $0"|"ABC.txt} ' $VAR ???

well, which one is a better way, sed or awk?

Thanks
Nimi


0

Response Number 3
Name: nails
Date: July 5, 2006 at 08:57:44 Pacific
Reply:

My original post assumed the actual filename was the replacement text. That's why I was able to use awk's FILENAME internal variable. You can embed shell variables in awk:

awk ' {
print $0"|""'"$VAR"'"
} ' ABC.txt

but I prefer to pass the variable's value to the awk script:

awk ' {
print $0"|"v1
} ' v1="$VAR" ABC.txt

In this case, which is better - awk or sed? It's a personal preference. I prefer the sed version.



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Adding variable at end of the row

Cron Job setting from 9th to end of the www.computing.net/answers/unix/cron-job-setting-from-9th-to-end-of-the-/3196.html

Inserting a string at the end of a www.computing.net/answers/unix/inserting-a-string-at-the-end-of-a-/6205.html

Problem with sp character at end of line www.computing.net/answers/unix/problem-with-sp-character-at-end-of-line/3039.html