Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

bash - Add at the end of the line with sed

given a plain text document with several lines like:

c48 7.587 7.39
c49 7.508 7.345983
c50 5.8 7.543
c51 8.37454546 7.34

I need to add some info 2 spaces after the end of the line, so for each line I would get:

c48 7.587 7.39  def
c49 7.508 7.345983  def
c50 5.8 7.543  def
c51 8.37454546 7.34  def

I need to do this for thousands of files. I guess this is possible to do with sed, but do not know how to. Any hint? Could you also give me some link with a tutorial or table for this cases?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

if all your files are in one directory

sed -i.bak 's/$/  def/' *.txt

to do it recursive (GNU find)

find /path -type f -iname '*.txt' -exec sed -i.bak 's/$/  def/' "{}" +;

you can see here for introduction to sed

Other ways you can use,

awk

for file in *
do
  awk '{print $0" def"}' $file >temp
  mv temp "$file"
done 

Bash shell

for file in *
do
  while read -r line
  do
      echo "$line def"
  done < $file >temp
  mv temp $file
done

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...