Adding variable value in the begining of all lines present in a file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding variable value in the begining of all lines present in a file.
# 1  
Old 04-15-2013
Wrench Adding variable value in the begining of all lines present in a file.

I am getting the varible value from a grep command as:
var=$(grep "Group" File1.txt | sed 's/Group Name[ ]*//g;s/,//g;s/://g;s/-//g')

which leaves me the value of $var=xyz.
now i want to append $var value in the begining of all the lines present in the file. Can u please suggest?
Input file:
1
2
3
4
5
Output File:
xyz1
xyz2
xyz3
xyz4
xyz5
# 2  
Old 04-15-2013
Code:
awk -v V="$var" '{$0=V $0}1' file.txt

# 3  
Old 04-15-2013
Code:
sed "s/^/$var/g" file.txt

# 4  
Old 04-16-2013
Quote:
Originally Posted by Yoda
Code:
awk -v V="$var" '{$0=V $0}1' file.txt


doesnt works.. isnt appending anything to the file Smilie

---------- Post updated at 12:14 PM ---------- Previous update was at 12:13 PM ----------

Quote:
Originally Posted by PikK45
Code:
sed "s/^/$var/g" file.txt


nothing got appended
# 5  
Old 04-16-2013
Both versions work exactly as expected. Is your var set? Is your input file a unix file or does it contain weird control chars?
# 6  
Old 04-16-2013
Quote:
Originally Posted by RudiC
Both versions work exactly as expected. Is your var set? Is your input file a unix file or does it contain weird control chars?

yes.. i have checked several times. echo $var gives me xyz as output.
As i mentioned I am populating $var through a grep command
var=$(grep "Group" F5 | sed 's/Group Name[ ]*//g;s/,//g;s/://g;s/-//g')

when i use the variable as a static variable by hardcoding var="xyz", every command works absolutely fine. but when i populate the variable dynamically using grep command it fails.. Smilie

file has no control characters.. simple txt file converted from a excel file
# 7  
Old 04-16-2013
If the commands work when used with var=xyz, then I'd bet your "dynamic assignment" kills the code. Sure the file being grepped has NO <CR> characters?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print lines present in first that match second variable. And ones that does not exist in second.

I have multi line input(var1) and reference(var2) variables. How to capture lines not present in var2 but present in var1? How to capture lines present var2 but not in var1? # configuration from server var1=""" Custom JAX-RS Custom Shared Web 2.0 """ # required configuration... (6 Replies)
Discussion started by: kchinnam
6 Replies

2. UNIX for Dummies Questions & Answers

Adding the balances present in UNIX file

I have a file which gets generated after successful completion of unix job and is in below format like Account Number Action Taken Balances 19905236 Posted to suspense 52638 The records in file runs in several thousand count . I want to add the... (2 Replies)
Discussion started by: er_raman1986
2 Replies

3. Shell Programming and Scripting

File Comparison: Print Lines not present in another file

Hi, I have fileA.txt like this. B01B02 D0011718 B01B03 D0012540 B01B04 D0006145 B01B05 D0004815 B01B06 D0012069 B01B07 D0004064 B01B08 D0011988 B01B09 D0012071 B01B10 D0005596 B01B11 D0011351 B01B12 D0004814 B01C01 D0011804 I want to compare this against another file (fileB.txt)... (3 Replies)
Discussion started by: genehunter
3 Replies

4. UNIX for Advanced & Expert Users

query display number lines or records present in file only numeric value -without filename

Hi all Thanks in advance........... Please help me for this issue............ I have a file it has 11 records . I used the command like .... >$ wc -l file 11 file I'm getting output like 11 file (no.of records along with filename) here my requirement is, I want to display only... (3 Replies)
Discussion started by: ksrivani
3 Replies

5. Shell Programming and Scripting

Appending number of lines in the file to begining of the file

I am having 6 files named file1,file2....file6 and i need to append number of lines in each file to begining of the file. For example, If file 1 contains a b c d then after adding new line file1 should contain 4 a b c d Thanks in advance. (2 Replies)
Discussion started by: akhay_ms
2 Replies

6. UNIX for Dummies Questions & Answers

To get the lines that are not present in file

Hi I have got two files File1: Row1 Row2 Row3 Row4 File2: Row3 Row4 Now my requirement is search each and every line of file1 in file2 and if the record do not exist in file2 then write that to an output file. Output file should be as below Row1 Row2 (4 Replies)
Discussion started by: sbhuvana20
4 Replies

7. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

8. UNIX for Dummies Questions & Answers

Extracting lines present in one file but missing in another using Perl

Hey I have an input file containing a list of numbers like: U01120.CDS.1 D25328.CDS.1 X15573.CDS.1 K03515.CDS.1 L44140.CDS.10 U24183.CDS.1 M97347.CDS.1 U05259.CDS.1 And another input file containing results created on the basis of the above input: G6PT_HUMAN U01120.CDS.1 -1.9450 3.1706... (1 Reply)
Discussion started by: Banni
1 Replies

9. Shell Programming and Scripting

How can I ignore only the lines which have # at the begining?

From the below file I want to grep only the lines except the comment sections. But grep -v "#" is eliminating the last line because it has one # in between. Any idea how can I ignore only the lines which have # at the begining (I mean comment lines) ? Thanks a lot to all in advance C Saha (1 Reply)
Discussion started by: csaha
1 Replies

10. Shell Programming and Scripting

Ignore Lines Begining With #

Is there a standard way to make a shell script read a file, or list, and skip each line that contains # at the begining, or ignores the content starting after a # in line? I'm looking to mimic the way commenting in a shell script normally works. This way I can comment my text files and lists my... (4 Replies)
Discussion started by: sysera
4 Replies
Login or Register to Ask a Question