Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 952
  • Last Modified:

Append data to the end of each line in a file

I have file called XXX

I need to appened some information to the end of each line in  file

Please provide command for that
0
sumanth_ora
Asked:
sumanth_ora
  • 6
  • 5
  • 2
  • +2
1 Solution
 
omarfaridCommented:
Hi,

Please try

#!/bin/sh
ed $1 << END
1,$ s/$/more data/
w
q
END

you may call the above script myshell and after you make it executable (chmod +x myshell), you may use it as:

./myshell myfile
0
 
TintinCommented:
sed "s/$/some new data/" file >file.new && mv file.new file
0
 
ozoCommented:
perl -i -pe 's/$/some new data/' XXX

perl -i -lpe '$_.="some new data"' XXX

some sed implementations may also have a -i switch
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ahoffmannCommented:
awk '{print $0" some new data"}' XXX
sed -e 's/.*/&" some new data"/' XXX
# just some more ...
0
 
sumanth_oraAuthor Commented:
awk '{print $0" $File_Date"}'

When I use above awk it's not picking value it's appending $File_Date to the end of each line

I want the acuval value to be append

here is the value  for $File_Date

,20071231 or |12_NOV_2007


===============================================================

sed "s/$/$File_Date/" file >file.new


I am getting message  for the above sed execution

sed garbled & makeing size of file.new as 0




0
 
omarfaridCommented:
Hi,

did you try the script provided?

0
 
sumanth_oraAuthor Commented:
Instead of wring in one more script I am trying like this

 ed $OutputFilename1<< END
             1,$ s/$/$File_Date/
             w
             q
             END
             
             cat OutputFilename1 >>$OutputFilename


I got the following err

syntax error at line 406 : `<<' unmatched
0
 
omarfaridCommented:
Hi,

The END should be at the beginning of the line (no space before it). Same for remaining commands 1,$ w & q

What is $File_Date ? is it an env variable defined in the script?

0
 
sumanth_oraAuthor Commented:
thats is the value i am getting dynamically
value might be ,11/22/2007 or |12_Nov_2007

Now give me example for ed using file _date
0
 
omarfaridCommented:
Hi,

OK

Now the data you are inserting or appending has slashes (/) in it, which confuse ed or sed. So use the other format with (_)

Now, the example

File_Date=12_Nov_2007
export File_Date
ed $OutputFilename1<< END
1,$ s/$/$File_Date/
w
q
END
0
 
sumanth_oraAuthor Commented:
I can't change the format...
requirement is like that
0
 
omarfaridCommented:
Hi,

File_Date='11\/22\/2007'
export File_Date
ed $OutputFilename1<< END
1,$ s/$/$File_Date/
w
q
END

Please note the single quotes and the back slashes
0
 
sumanth_oraAuthor Commented:
but you are providing hard coded value..
0
 
omarfaridCommented:
Hi,

Yes, but in your case you need to reformat the data, e.g. add the line below before ed command

File_Date=`echo $File_Date | awk -F"/" '{print $1 "\/" $2 "\/" $3}'`
0
 
ahoffmannCommented:
> .. value might be ,11/22/2007 or |12_Nov_2007
that's the reason for the "sed garbled command", simply remove the / for sed's s by something else, i.e.
  s:$:31/11/2007:

awk '{print $0"'"$File_Date"'"}' XXX

but you accepted something else already ...
0
 
TintinCommented:
knowing the data is important.  You could have done

sed "s#\$#$File_Date#/" file >file.new && mv file.new file
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now