Removing spaces from line matching a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing spaces from line matching a pattern
# 1  
Old 07-17-2014
Removing spaces from line matching a pattern

Hi,

I want to remove the spaces from all the lines matching a particular pattern from my file. For instance in file abc.txt I have following data.
Code:
Header,This is the header
111,this is 1st record
222, this is 2nd record
333, this is 3rd record
Footer,3 records found
Footer,111222333

In this data, the 1st Footer has spaces between "3", "records" and "found" and the 2nd footer has trailing spaces after "111222333". I want to remove the spaces only from the records which starts with keyword "Footer".
Expected Output 1:
Code:
Header,This is the header
111,this is 1st record
222, this is 2nd record
333, this is 3rd record
Footer,3recordsfound
Footer,111222333

Also, is there a way I can just remove the trailing spaces from matched rows?
Expected Output 2:
Code:
Header,This is the header
111,this is 1st record
222, this is 2nd record
333, this is 3rd record
Footer,3 records found
Footer,111222333

# 2  
Old 07-17-2014
Try something like:
Code:
sed '/Footer/s/ //g' file

I don't get the the 2nd output, which is identical to the input..
# 3  
Old 07-17-2014
Thanks!

Quote:
I don't get the the 2nd output, which is identical to the input..
2nd output actually looks identical but the input has trailing spaces in the last record whereas the output doesn't have any trailing spaces. Thats the difference.
# 4  
Old 07-17-2014
OK, the sed command should get rid of those as well...


--
If there can also be TABs you could try:
Code:
sed '/Footer/s/[[:blank:]]//g' file

or
Code:
awk '/Footer/{$1=$1}1' OFS= file

# 5  
Old 07-17-2014
Thank you Scrutinizer ... took a lead from your example and came up with below.

To remove all spaces from the row that begins with keyword "Footer"
Code:
sed '/^Footer/s/ //g' file1 > file2

To remove only the trailing spaces from the row that begins with keyword "Footer"
Code:
sed '/^Footer/s/ *$//g' file1 > file2

# 6  
Old 07-17-2014
Quote:
Originally Posted by decci_7
... ... ...
To remove only the trailing spaces from the row that begins with keyword "Footer"
Code:
sed '/^Footer/s/ *$//g' file1 > file2

Note that when you're dealing with an anchored expression (i.e., ^expression or expression$), the g flag is superfluous. The command:
Code:
sed '/^Footer/s/ *$//' file1 > file2

produces exactly the same results as the command above.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing tab spaces at the end of each line

I have a file which contains the data lines like below.I want to remove the tab spaces at the end of each line.I have tried with the command sed 's/\+$//' file.but it does not work.Can anyone help me on this? 15022 15022 15022 15022 15022 15022 15023 15023 15023 15023 15023 ... (16 Replies)
Discussion started by: am24
16 Replies

2. Shell Programming and Scripting

Removing a pattern in a line

Dear team, I have a file curve.csv which is generated from oracle and each line has a comment associated with it, I want to get rid of this comment, can you please suggest me a command as how to do it Eg, cat curve.csv /*data for today curve*/ /*data for text1*/ this is the header /*data... (6 Replies)
Discussion started by: infyanurag
6 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Matching some string in a line and removing that

I have one output file. Node: hstg1so Date: 2013/07/16 17:51:24 GMT Totals: 10608 6871 0 2208 1529 0 0 64% 0% ( 0 ) Node: hstg2so Date: 2013/07/16 17:51:25 GMT Totals: ... (3 Replies)
Discussion started by: Raza Ali
3 Replies

5. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

6. Shell Programming and Scripting

Removing files matching a pattern

I am on ubuntu 11.10 using bash scripts I want to remove all files matching a string pattern and I am using the following code find . -name "*$pattern*" -exec rm -f {} \;I have encountered a problem when $pattern is empty. In this case all my files in my current directory were deleted. This... (3 Replies)
Discussion started by: kristinu
3 Replies

7. Shell Programming and Scripting

Deleting pattern without removing line

I am trying to delete a pattern without removing line. I searched a lot in this forum and using those I could come up with sed command but it seems that command does not work. Here's how my file looks like: 1 ./63990 7 1171 ./63990 2 2425 ./63990 9 2539 ./63990 1 3125 ./63990 1 10141... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

8. Shell Programming and Scripting

Removing data with pattern matching

I have the following: HH:MM:SS I want to use either % or # sign to remove :SS can somebody please provide me an example. I know how to do this in awk, but awk is too much overhead for something this simple since I will be doing this in a loop a lot of times. Thanks in advance to all... (2 Replies)
Discussion started by: BeefStu
2 Replies

9. Shell Programming and Scripting

Removing spaces in a line

Hi All, I have a line like this " field1;field2;field3 " (single space after and before double quotes). Now i have to remove these single space . Kindly help me. Thanks in advance (2 Replies)
Discussion started by: krishna_gnv
2 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question