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
Need to serach if a new line character exists on the last line in a file sunilbm78 UNIX for Dummies Questions & Answers 10 02-29-2008 02:15 PM
batch delete using find, files with # character tphyahoo UNIX for Dummies Questions & Answers 0 05-30-2006 01:05 PM
Find files which contain a null character Bab00shka UNIX for Dummies Questions & Answers 5 12-16-2005 10:41 AM
delete a line based on first character of the line borncrazy UNIX for Dummies Questions & Answers 2 12-06-2005 03:27 PM
end of the line character zomboo Shell Programming and Scripting 5 12-30-2004 05:30 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-24-2008
dbsurf dbsurf is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 23
need to find last character in each line in all files

Hi,

I need to read each line in all files .If last character in each line is not ;(semicolon) then i need to append next line also.Here each line may consists of multiple semicolons(.but i need to read only last character.I need to achieve this using shell script.And i need to remove tht last semicolon also.I need to achieve this in all files in a directory and i need to redirect modified files to the same directory.
How to achieve this?

For example my file consists of
asdfgh;adffg;afdfg;
adfsffvfgggg;
aedfdsfs
fsff;

Here i need output as
asdfgh;adffg;afdfg
adfsffvfgggg
aedfdsfs fsff

How to achieve this?
  #2 (permalink)  
Old 01-24-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
if you have GNU sed:
Code:
sed -i 's/;$//' file*

or using Perl:
Code:
perl -pi -e 's/;$//' file*

  #3 (permalink)  
Old 01-24-2008
dbsurf dbsurf is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 23
Hi,

If there is no semicolon its not appending next line with present line.And also its not working if my file contains pipe line characters like '|'.

For example if file contains

user|find|ghjk|gg|;
sufgh|fghj|hgj
Dhj|hjk|jkl;
its not removing semicolon
and its not appending 3red line to 2nd line
  #4 (permalink)  
Old 01-24-2008
quintet quintet is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 95
try this

sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' file
  #5 (permalink)  
Old 01-24-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926

Code:
awk 'ORS=/;$/?RS:FS{sub(/;$/,"")}1' filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
  #6 (permalink)  
Old 01-24-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
As far as the entire task is concerned:


Code:
awk 'FNR == 1 {
  system("[ -f \""fnt"\" ]&&mv "fnt" "fn)
  fn = FILENAME
  fnt = FILENAME"__"++c
}
ORS = /;$/ ? RS : FS {
  sub(/;$/,"")
  print > fnt
} END {
  system("[ -f \""fnt"\" ]&&mv "fnt" "fn)
}' filenames*

Use nawk or /usr/xpg4/bin/awk on Solaris.
  #7 (permalink)  
Old 01-24-2008
dbsurf dbsurf is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 23
Hi quintet

using this sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' file its not redirecting to my directory again
Closed Thread

Bookmarks

Tags
perl, sed

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 03:45 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