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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace nth value in xml file subin_bala Shell Programming and Scripting 4 06-18-2008 03:39 PM
replace words in file based on another file kinmak Shell Programming and Scripting 9 05-07-2008 06:06 AM
Replace word in a file sasiharitha UNIX for Dummies Questions & Answers 3 12-05-2007 01:11 AM
serach and replace file name in the path in a remote xml file kiranreddy1215 Shell Programming and Scripting 1 11-12-2007 11:31 AM
Replace a Value in File lesstjm Shell Programming and Scripting 5 05-16-2007 02:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-14-2008
senthil_is senthil_is is offline
Registered User
  
 

Join Date: Sep 2007
Location: singapore
Posts: 35
Replace the file using sed

I have file with 70 lines.

I want to add 'SLV' at the starting of the line if the line has first character as 0 or 1 or 2 or ..9

Nothing else has to be changed in the file. Please anyone advice.
  #2 (permalink)  
Old 01-14-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798

Code:
sed -e "s/^\([0123456789]\)/SLV \1/g" file.txt

  #3 (permalink)  
Old 01-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
shell

Code:
#!/bin/sh
while IFS= read -r line
do
 case $line in 
   [0-9]* ) printf "%s\n" "SLV $line";;
    *) printf "%s\n" "$line";;
 esac
done < "file" > newfile

  #4 (permalink)  
Old 01-14-2008
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,245

Code:
sed 's/^[0-9]/SLV &/' file.txt

  #5 (permalink)  
Old 01-14-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
using Perl:
Code:
perl -pi -e 's/^(\d)/SLV \1/' file

  #6 (permalink)  
Old 11-20-2008
lahiru lahiru is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 4
Check with this

Quote:
Originally Posted by senthil_is View Post
I have file with 70 lines.

I want to add 'SLV' at the starting of the line if the line has first character as 0 or 1 or 2 or ..9

Nothing else has to be changed in the file. Please anyone advice.
=======================================================

#!/usr/bin/ksh

ls -l |grep .txt|nawk '{print $9}' > list

while read FILE

do

perl -pi -e 's/01/09\/SWV/g' $FILE

done <list
Closed Thread

Bookmarks

Tags
perl, sed, shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0