bash: delete a string from last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash: delete a string from last line
# 8  
Old 10-11-2011
Quote:
Originally Posted by Chella15
@ygemici:

thank you for your reply, as I have already mentioned I am able to do with the file say new.html (your file). But I am not yet able to achieve it for a variable got from the while loop.
If you have a similar solution for variable instead of filename, that will be really helpful.

I tried things like below to replace the string with empty which did not work. Basically i wanted to remove that string from the last line
sed '$s/'\''||chr(9)||/ /g' data > tmpfile.txt
sed '$s/'\''||chr(9)||/ /g' $data > tmpfile.txt
sed '$s/'\''||chr(9)||/ /g' ${data} > tmpfile.txt
Code:
# cat infile
first line
second line
third line '||chr( do not change this ok |(*0linuxisnotunix
last linee '||chr( change this ok |(*&^%

Code:
x=`wc -l <infile`;while read -r line; do [[ $x -eq 1 ]] && newlastline=$(echo $line|sed "s;'||chr(; ;");((x--));done<infile
echo "newlastline=$newlastline"
newlastline=last linee   change this ok |(*&^%

if you update your file with new last line
Code:
# >newfile;x=`wc -l <infile`;while read -r line; do 
[[ $x -eq 1 ]]&&newlastline=$(echo $line|sed "s;'||chr(; ;")&&echo "$newlastline">>newfile||echo "$line">>newfile
((x--));done<infile;more newfile
first line
second line
third line '||chr( do not change this ok |(*0linuxisnotunix
last linee   change this ok |(*&^%

regards
ygemici

Last edited by ygemici; 10-11-2011 at 03:05 PM..
# 9  
Old 10-11-2011
Thanks ygemici !

This works great, but I am not sure how to adapt this into my requirement as I had to manipulate it after the loop and not within. Can you also please explain the below part of the code :
Code:
sed "s;'||chr(; ;");

In my requirement I dont have a specific line number. So it will be easy if it can be manipulated after the loop is finished.

Last edited by Franklin52; 10-13-2011 at 04:26 AM.. Reason: Please use code tags, thank you
# 10  
Old 10-11-2011
Quote:
Originally Posted by Chella15
Thanks ygemici !

This works great, but I am not sure how to adapt this into my requirement as I had to manipulate it after the loop and not within. Can you also please explain the below part of the code :
sed "s;'||chr(; ;");
In my requirement I dont have a specific line number. So it will be easy if it can be manipulated after the loop is finished.
Code:
# cat infile
first line
second line
third line '||chr( do not change this ok |(*0linuxisnotunix
last linee '||chr( change this ok |(*&^%

actually , you can find with easily grep the your string which is the lines without any loop
Code:
# grep "'||chr(" infile -n|cut -d: -f1
3
4

then
you can change at 3.line your string with a space you can use this
Code:
# sed "3s;'||chr(; ;" infile

you can change at 4.line your string with a space you can use this
Code:
# sed "4s;'||chr(; ;" infile

Code:
"3s;'||chr(; ;"

Code:
//a little explanation//
" -> sed cmd begin
3s -> only edit 3 line
; -> sed exp separator
'||chr( -> your pattern
; ; --> this a space instead of your pattern (that changed with your pattern) [between semicolons]
; -> sed separator that is close sed cmd
" -> sed cmd finish (actually we tell our shell what is the sed parameters to called sed with these by shell)

# 11  
Old 10-13-2011
@ygemici

Sorry for the late reply... i was trying a couple of other things too.. Smilie
When i tried to grep with a file name what u said below works... but it doesnt work with a variable name. Were u able to successfully execute this ? Is infile considered like a variable name ?
# grep "'||chr(" infile -n|cut -d: -f1
grep "'||chr(" new.html -n |cut -d: -f1 > new1.html works fine.

Also i did - man grep and the syntax seems to accept only file name.

With SED (accepts file and pipeline inputs), i was able to use a variable name but it wasnt able to replace the last line.. instead it replaced all lines...
This is what i used ( also tried simple examples)
name=`echo $data | sed '$s/'\''||chr(10)||/ /g'`
Expectation:
$s would replace in the last line
replace '||chr(10)|| with blank

Note: in my file there are more than one line that has that string similar to your example.
# 12  
Old 10-13-2011
Quote:
Originally Posted by Chella15
@ygemici
With SED (accepts file and pipeline inputs), i was able to use a variable name but it wasnt able to replace the last line.. instead it replaced all lines...
This is what i used ( also tried simple examples)
name=`echo $data | sed '$s/'\''||chr(10)||/ /g'`
Expectation:
$s would replace in the last line
replace '||chr(10)|| with blank

Note: in my file there are more than one line that has that string similar to your example.
try this..
Code:
# data=`sed -n '$p' infile` ## take only last line
# name=`echo "$data" | sed '$s/'\''||chr(10)||/ /g'` ## change your pattern(s) with a space
# echo "$name" ## write your new value

regards
ygemici
# 13  
Old 10-13-2011
Lightbulb

@ygemici:

Thanks for your effort and time.Smilie
I was working in the similar way, there were couple of glitches which i just fixed:
1. I didnt notice that my while loop was appending extra line at the end, by which it wasnt able to replace in the last line
2. Also the variable has to be "$var name"

My code that worked:
Code:
#here it deletes the extra line added after the while loop
var1=`echo "$data" | sed '$d'`

#here it replaces as required
Replaced_Var=`echo "$var1" | sed '$s/'\''||chr(10)||//g'`

I have a few questions if anyone can answer or correct me:
1. Why is there always a line added after a while loop or when using a SED on a file or variable ? Any wise way of handling it ? Currently am deleting the last empty line
2. With grep we can use only file names , so we cannot use variables on it
3. With SED we can use file names or pipe-lined as my code. A variable name as such is not acceptable unless piped.

Last edited by Chella15; 10-13-2011 at 06:26 PM.. Reason: gramatical
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find string and delete before just in line?

Hello, When my lines contain question mark, I use below command to delete the portion of the matching line coming after question mark: sed 's/?.*//' SampleFile SampleFile: helloworldfirstline?mdksmyymsss hellosecondlineworld?mdksmkkmsss thirdhelloworld?mdksmccmsss Output:... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Delete all lines except a line starting with string

Shell : bash OS : RHEL 6.8 I have a file like below. $ cat pattern.txt hello txt1 txt2 txt3 some other text txt4 I want to remove all lines in this file except the ones starting with txt . How can I do this ? (4 Replies)
Discussion started by: omega3
4 Replies

3. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

4. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

5. Shell Programming and Scripting

Delete the last line if match the string

Hi, How to delete the last line if is match the below string else no action... String checking "END OF FILE. ROW COUNT: " 9f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|2 9f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|0 229f680174-cb87-4f71-887a-93b6f62fa5aa|20077337254|3 END OF... (2 Replies)
Discussion started by: bmk
2 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Search for a line, delete a string in it

let me start out by saying i have ZERO exp with any kind of scripting, so sorry if this is really basic stuff..... For example, I need to have a script that will search a file and find this line in the file: *.cat;dog;kennel;house;barn;horse;hay;coat hat and remove the "coat" from the... (12 Replies)
Discussion started by: skunky
12 Replies

8. Shell Programming and Scripting

search string and delete the line

Hi All, I have a file from Mainframe which has one of the lines with so many words... i tried to fold, format to 80 chararcter.. stil did not work. So i have decided to search for a string in that line Ex.FLIGHT PLAN and once if it is found i want to delete the entire line. Please help... (2 Replies)
Discussion started by: digitalrg
2 Replies

9. Shell Programming and Scripting

delete a string from line

hi i have a file contains data session terminated Line timeout: 120 Change state START->SIGNON_REPLY, RC=0 Change state SIGNON_REPLY->SIGNON, RC=0 i need to remove "Change state" and write to the same file please help thanks Satya (3 Replies)
Discussion started by: Satyak
3 Replies

10. Shell Programming and Scripting

Delete a Line with a string X

Hi guys, I am new here. I have done many scripts in VisualBasic and batch. Now going to *inux and learning bash and perl, quite amazing commands I find in linux, like sed, grep, awk and so on... the task is simple, find a string in folders and remove the line where that string exists. I can... (3 Replies)
Discussion started by: 4scriptmoni
3 Replies
Login or Register to Ask a Question