need to find last character in each line in all files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to find last character in each line in all files
# 1  
Old 01-24-2008
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(Smilie.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  
Old 01-24-2008
if you have GNU sed:
Code:
sed -i 's/;$//' file*

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

# 3  
Old 01-24-2008
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  
Old 01-24-2008
try this

sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' file
# 5  
Old 01-24-2008
Code:
awk 'ORS=/;$/?RS:FS{sub(/;$/,"")}1' filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 6  
Old 01-24-2008
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  
Old 01-24-2008
Hi quintet

using this sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' file its not redirecting to my directory again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: copying lines with specific character to files with same name as copied line.

I am trying to make my script as simple as a possible but, I am not sure if the way I am approaching is necessarily the most efficient or effective it can be. What I am mainly trying to fix is a for loop to remove a string from the specified files and within this loop I am trying to copy the lines... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

2. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

3. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

4. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

7. Solaris

Find files contains specific line

Hi, I need a command that could search the specified directory (and its sub directories) and file contents. In the result display path of the file and string The command: find <search_dir> -exec grep <search_string> {} \; works but doesn't show the file name Thanks (2 Replies)
Discussion started by: Mack1982
2 Replies

8. Shell Programming and Scripting

search directory-find files-append at end of line

Hi, I have a command "get_data" with some parameters in few *.text files of a directory. I want to first find those files that contain this command and then append the following parameter to the end of the command. example of an entry in the file :- get_data -x -m50 /etc/web/getid this... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

9. UNIX for Dummies Questions & Answers

batch delete using find, files with # character

UPDATE: Sorry, disregard this. It did work, I made a mistake; I just shouldn't have been using maxdepth. I do think it is good to know, however, that find | grep '#' | xargs rm will "clean up" funnily named files in a directory. Of course, some of those funnily named files are there... (0 Replies)
Discussion started by: tphyahoo
0 Replies

10. UNIX for Dummies Questions & Answers

Find files which contain a null character

Hi, I would like to use grep to find files which contain NULL characters. I'm not sure how to represent the null character in the grep statement. Could anyone help please? Thanks! Helen :confused: (5 Replies)
Discussion started by: Bab00shka
5 Replies
Login or Register to Ask a Question