best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)
# 15  
Old 08-26-2010
before the sed:

Code:
richard@opensolaris:~/share/shell/ac# wc -l test_big.log
  447940 test_big.log

after the sed:

Code:
richard@opensolaris:~/share/shell/ac# wc -l test_big.log
  447940 test_big.log

That's weird - it's showing the same no. of lines but it definately seems to be striping out the last line - i did a test by just adding a new line at the end of the text file "has this line been deleted" and after running the sed function had ran, that line had indeed been deleted???

Smilie

Last edited by Scott; 08-26-2010 at 01:57 PM.. Reason: Added code tags
# 16  
Old 08-26-2010
Smilie

I just tried on Solaris 10. If the last line has no new line, it deletes the line.

OSX:
Code:
$ printf "1\n2\n3\n4" > file1
$ sed "s/3/33/" file1
1
2
33
4

Solaris:
Code:
# printf "1\n2\n3\n4" > file1
# sed "s/3/33/" file1
1
2
33

# echo >> file1
# sed "s/3/33/" file1
1
2
33
4

You mentioned OpenSolaris in an earlier post - I missed that

Interestingly (or not Smilie), using ed:

Code:
# ed file1 << !
> 1,$ s/3/33/
> w
> q
> !
'\n' appended
8
9
# cat file1
1
2
33
4


Last edited by Scott; 08-26-2010 at 01:31 PM..
# 17  
Old 08-26-2010
also if try want to write last line without \n is return anything

Code:
sed -n "$p" file1

equal null
unfortunately solaris sed has a few bugs SmilieSmilie
# 18  
Old 08-26-2010
Quote:
Originally Posted by ygemici
also if try want to write last line without \n is return anything

Code:
sed -n "$p" file1

equal null
unfortunately solaris sed has a few bugs SmilieSmilie
Hi.

I would (should) expect the same behaviour on AIX as I get on Solaris (but don't have access to it to try now). Perhaps the GNU sed is just too nice to us? Smilie

This:
Code:
$ sed -n "$p" file1

is the same as
Code:
$ sed -n "" file1

if p is not set, as $p would be expanded by the shell (as a variable).

Try:
Code:
$ sed -n "$ p" file1

or
Code:
$ sed -n '$p' file1

(although it probably won't print anything on Solaris if there's no newline there!)
# 19  
Old 08-26-2010
Quote:
Originally Posted by scottn
Hi.

I would (should) expect the same behaviour on AIX as I get on Solaris (but don't have access to it to try now). Perhaps the GNU sed is just too nice to us? Smilie

This:
Code:
$ sed -n "$p" file1

is the same as
Code:
$ sed -n "" file1

if p is not set, as $p would be expanded by the shell (as a variable).

Try:
Code:
$ sed -n "$ p" file1

or
Code:
$ sed -n '$p' file1

(although it probably won't print anything on Solaris if there's no newline there!)
i think same you about gnu sed Smilie

but these are all the same and also return null
Code:
$ sed -n "$ p" file1

or
Code:
$ sed -n '$p' file1

for this we can use like this Smilie
Code:
# cat file1
1
2
3
4

trick this for me Smilie
Code:
 
/usr/xpg4/bin/sed -n '$p' file1 2>/dev/null
4

Code:
/usr/xpg4/bin/sed "" file1 2>/dev/null
1
2
3
4

or

Code:
/usr/xpg4/bin/sed '' file1 2>/dev/null
1
2
3
4

# 20  
Old 08-27-2010
bummer! bit of a pain that Smilie as I may need to replace some strings in the last line of a log file but not actually delete the last line of the log file because it may contain other info I want Smilie

I just realised I posted this thread in the wrong forum! it was meant to be in the shell programming and scripting one doh!

Last edited by rich@ardz; 08-27-2010 at 12:38 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with search and replacing multiple items in multiple files

Im having an issue when trying to replace the first column with a new set of values in multiple files. The results from the following code only replaces the files with the last set of values in val.txt. I want to replace all the files with all the values. for date in {1..31} do for val in... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

2. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

3. Shell Programming and Scripting

Grep strings on multiple files and output to multiple files

Hi All, I want to use egrep on multiple files and the results should be output to multiple files. I am using the below code in my shell script(working in Ksh shell). However with this code I am not attaining the desired results. #!/bin/ksh ( a="/path/file1" b="path/file2" for file in... (4 Replies)
Discussion started by: am24
4 Replies

4. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

5. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

6. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

7. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

8. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

9. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

10. Shell Programming and Scripting

Replacing text from multiple files at multiple location

Hi, I have many files scattered in all different folders. I want to replace the text within all the files using a single command ( awk, sed...) Is it possible? example find all the files in which there is text "memory" and replace it with "branded_memories". the files can be at the... (2 Replies)
Discussion started by: rudoraj
2 Replies
Login or Register to Ask a Question