The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Jun 2007
Location: Mumbai,India
Posts: 97
sed help.

frnds,

I have lots of files .. the contents of these files are semicolon delimited.
like

Cmpabc;20080215;20080215082141;20080215082812;199483;43188746

i want to insert 1 more value in the beginning... ( there are 2 options for this value depends on the file name...)

I 'll handle that with if.. ( m i right ??? )

so , like after the operation ..the content sud be

X;Cmpabc;20080215;20080215082141;20080215082812;199483;43188746


more details lets i have 2 files X_ABC.dat and Y_ABC.dat
so i want to insert X for the first file n Y for the second ....

pl help...
-anchal.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Aug 2007
Posts: 40
Code:
word=whatever
{
  IFS= read -r line
  printf "%s %s\n" "$word" "$line" 
  cat
} < FILENAME > NEWFILE
Reply With Quote
  #3 (permalink)  
Old 03-20-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,035
This problem looks awful, but is quite simple once you know that you can address the whole matched expression by a "variable": "&". Here is an example:

Code:
print "hello world" | sed 's/^.*$/=&=/'
When you try that you will see that "hello world" is now surrounded by equal signs. The expression "^.*$" matches whole lines regardless of their content.

Hence, if you want to insert "X;" at the beginning of the line:

Code:
sed 's/^.*$/X;&/' /your/file
If you do that inside a script you might hold the text you want to insert in some shell variable. Then you will have to expand the variable inside the script:

Code:
sed 's/^.*$/'"$var"';&/' /your/file
Fill the variable $var with whatever you want to add at the beginning of the lines.

I hope this helps.

bakunin
Reply With Quote
  #4 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Jun 2007
Location: Mumbai,India
Posts: 97
hey bakunin...

I have roughly tried these commands ... these are working well...
'll try to implement in my script. [ hope I'd solve the other probs,,, :-) ]

thanks for the help..

thanks 2 all..
Reply With Quote
  #5 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Mar 2006
Location: Mumbai
Posts: 67
How abt this:

echo "hello world" | awk '{ print "X;"$0}'
X;hello world
Reply With Quote
  #6 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Mar 2006
Location: Mumbai
Posts: 67
One more using remembering patterns

echo "hello world" | sed 's/\(.*\)/X;\1/'
X;hello world
Reply With Quote
  #7 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Jun 2007
Location: Mumbai,India
Posts: 97
frnds..,
I m lltle messed up while implementing this logic.... :-(

actually... as i already wrote here that I want to prepend "X;" only if filenames contains
X. and "Y;" if filenames contains Y.

this is what i did.. ( code shown for this part only.. )


for FILENAME in `ls -1 $FileDir/ABC_?_123.tmp`
do
FOUND=`ls -1 $FileDir/ABC_?_123.tmp | grep X`
if [ "$FOUND" ]
then
TMP=tmpfile_$$
sed -e 's/^.*$/X;&/' < $FILENAME > $TMP
mv $TMP $FILENAME
else
echo "The file $FILENAME does not include the word X"
fi
done



now what happenps is.. I got all my files with "X;" prepended to them.., which comes under the for loop condition...


what cud be the prob?
or can any1 suggest any other logic??

thanks..
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0