Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding text 1

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hello again.....

Thought I better start a new thread. Using help from this forum I have managed to get to a file that looks something like this:

;45;45;258;457;256
;125;56;485;45;65

The final step is this. How would I add a specified text (for example 'pingpong') to the front of every line? The end result looking like this:

pingpong;45;45;258;457;256
pingpong;125;56;485;45;65

...please... :)
 
Not too elegant, but:

for line in `cat filename`
do
echo pingpong$line >> newfile
done

Note that the ` are backticks not regular apostrophes. Cheers.
 
Try sed

Create a file called change.sed containing the following

#!/usr/bin/sed -f
s/^/pingpong/g

chmod 700 change.sed

then use

./change.sed < original.file > new.file

--
| Mike Nixon
| Unix Admin
| ----------------------------
 
nawk -f a.awk a.txt

#-------------------------- a.awk
FNR == 1 {printf(&quot;%s&quot;, &quot;pingpong&quot;)}
$0 ~ &quot;^$&quot; {print;printf(&quot;%s&quot;, &quot;pingpong&quot;);next}
{ printf(&quot;%s&quot;, $0); }
END { print}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
vlad - that doesn't seem to work...
 
a bit easier:

nawk -v RS=&quot;&quot; -f a.awk a.txt

#---------------------- a.awk
{ printf &quot;pingpong&quot;; for (i=1; i<=NF; i++) printf(&quot;%s&quot;, $i); print &quot;&quot;}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Or

nawk -f a.awk a.txt

#---------------------- a.awk
{print &quot;pingpong&quot; $0} CaKiwi
 
BIS,
what seems to be the problem?
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
sorry, my mistake - it does work...

last question, if I may.

I now have a file something like this:

bbbbbb
bbbbbb
bbbbbb
bbbbbb;45;45;45;45;
bbbbbb;45;45;45;45;
bbbbbb;45;45;45;45;

is there a way to remove the first 3 lines to end up with:
bbbbbb;45;45;45;45;
bbbbbb;45;45;45;45;
bbbbbb;45;45;45;45;


?

I can always use sed -e '1d' file three times but there must be a more elegant way...
 
if it's ALWAYS the first 3 lines:
sed '1,3d' file.txt

If it's anything else, you'll need to define the criteria a bit more precisely.
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Yes its always the first three lines. many many thanks (again).
 
hmm, a new thing crept up on me. I am going to have to prepend yesterdays date. I can easily get this into a txt file. can I use this?

#---------------------- a.awk
{ printf &quot;cat yesterday&quot;; for (i=1; i<=NF; i++) printf(&quot;%s&quot;, $i); print &quot;&quot;}

where yesterday is the txt file?

I guess not. How does awk, nawk, or is it printf print the contents of a txt file?
 
Can you get it into a variable instead ? it would be cleaner.

nawk -v RS=&quot;&quot; -v yesterday=&quot;${myYesterdayVar}&quot; -f a.awk a.txt

#---------------------- a.awk
{ printf yesterday ; for (i=1; i<=NF; i++) printf(&quot;%s&quot;, $i); print &quot;&quot;} vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
cooooool - you saved my day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top