Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

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

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #15  
Old Unix and Linux 08-26-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
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???

Linux

Last edited by Scott; 08-26-2010 at 12:57 PM.. Reason: Added code tags
Sponsored Links
    #16  
Old Unix and Linux 08-26-2010
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 24 June 2017, 9:56 AM EDT
Location: Zürich
Posts: 8,611
Thanks: 344
Thanked 1,101 Times in 952 Posts
Linux

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 Linux), 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 12:31 PM..
Sponsored Links
    #17  
Old Unix and Linux 08-26-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 26 April 2017, 8:59 AM EDT
Location: istanbul
Posts: 1,713
Thanks: 4
Thanked 295 Times in 286 Posts
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 LinuxLinux
    #18  
Old Unix and Linux 08-26-2010
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 24 June 2017, 9:56 AM EDT
Location: Zürich
Posts: 8,611
Thanks: 344
Thanked 1,101 Times in 952 Posts
Quote:
Originally Posted by ygemici View Post
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 LinuxLinux
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? Linux

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!)
Sponsored Links
    #19  
Old Unix and Linux 08-26-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 26 April 2017, 8:59 AM EDT
Location: istanbul
Posts: 1,713
Thanks: 4
Thanked 295 Times in 286 Posts
Quote:
Originally Posted by scottn View Post
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? Linux

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 Linux

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 Linux

Code:
# cat file1
1
2
3
4

trick this for me Linux

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

Sponsored Links
    #20  
Old Unix and Linux 08-27-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
bummer! bit of a pain that Linux 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 Linux

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 11:38 AM..
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing text from multiple files at multiple location rudoraj Shell Programming and Scripting 2 05-12-2010 04:16 PM
Replacing string in multiple files arulanandsp Shell Programming and Scripting 3 09-11-2009 12:35 AM
need help with post:extract multiple columns from multiple files manishabh Shell Programming and Scripting 0 08-19-2009 01:09 PM
extract multiple cloumns from multiple files; skip rows and include filenames; awk manishabh Shell Programming and Scripting 4 08-18-2009 10:55 PM
renaming multiple files while replacing string kevin9 Shell Programming and Scripting 2 05-10-2009 06:41 PM



All times are GMT -4. The time now is 09:57 AM.