Solved

Append data to the end of each line in a file

Posted on 2007-11-23
16
926 Views
Last Modified: 2013-12-27
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
Comment
Question by:sumanth_ora
  • 6
  • 5
  • 2
  • +2
16 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 20341504
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20341962
sed "s/$/some new data/" file >file.new && mv file.new file
0
 
LVL 84

Expert Comment

by:ozo
ID: 20342326
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20343002
awk '{print $0" some new data"}' XXX
sed -e 's/.*/&" some new data"/' XXX
# just some more ...
0
 

Author Comment

by:sumanth_ora
ID: 20343325
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20343339
Hi,

did you try the script provided?

0
 

Author Comment

by:sumanth_ora
ID: 20343371
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20343391
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sumanth_ora
ID: 20343426
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20343444
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
 

Author Comment

by:sumanth_ora
ID: 20343472
I can't change the format...
requirement is like that
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20343487
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
 

Author Comment

by:sumanth_ora
ID: 20343543
but you are providing hard coded value..
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 20343594
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20344968
> .. 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
 
LVL 48

Expert Comment

by:Tintin
ID: 20346724
knowing the data is important.  You could have done

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now