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.
# 8  
Old 04-16-2013
Quote:
Originally Posted by RudiC
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?

found contro characters at the end of each line in the file.. $
unable to remove it. do i have to rectreate the file?

---------- Post updated at 03:17 PM ---------- Previous update was at 01:27 PM ----------

Quote:
Originally Posted by RudiC
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?
Hi RudiC
here is what i have got:

Input File:
Group Name ::-,,,,,,,ADIDAS,,,,,,,,,,,,,,,,,,,,,,,,,
,54,,3607345380247,,,,,,,,,ADIDAS DEO FORCE 150ML&,,,,,,175 ,,,,,,166 ,,,1 ,,,,,166.25

Required output:
ADIDASGroup Name ::-,,,,,,,ADIDAS,,,,,,,,,,,,,,,,,,,,,,,,,
ADIDAS,54,,3607345380247,,,,,,,,,ADIDAS DEO FORCE 150ML&,,,,,,175 ,,,,,,166 ,,,1 ,,,,,166.25

To get adidas in a avaribale i am using following coomand:
variable=$(grep "Group" F5 | sed 's/Group Name[ ]*//g;s/,//g;s/://g;s/-//g')
works abslty fine. Not to append the value of this variable at the first position o the line m using
awk -v V="$variable" '{$0=V $0}1' F5
Output:

Group Name ::-,,,,,,,ADIDAS,,,,,,,,,,,,,,,,,,,,,,,,,
,54,,3607345380247,,,,,,,,,ADIDAS DEO FORCE 150ML&,,,,,,175 ,,,,,,166 ,,,1 ,,,,,166.25


No control chars.. checked throughout.. crreated a new file issue stil persist
# 9  
Old 04-16-2013
Which OS are you using?
# 10  
Old 04-16-2013
This got fixed.. actually grep was introducing a ^M charater in the variable.
Redirected the grep output in the file. did a dos2unix on the file.. took output from file in a variable and used in awk Smilie
thnx everyone specially RudiC. i wd have never thot abt cntrl chrctrs being the showstoppers.. Smilie
# 11  
Old 04-16-2013
grep would never do that. I suppose that is there in all the fields of your file :P
 
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