Sed save changes to same file in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed save changes to same file in loop
# 8  
Old 08-01-2010
MySQL

Quote:
Originally Posted by Scrutinizer
Hi, your original idea seems sound to me except for the double quotes as scottn suggested, plus you need a revised regex syntax so that the operations are restricted to the intended fields:

Code:
var="1;2"
IFS=";" 
for i in $var
do
    sed "s/[^;]*;/;/$i" $file  > $temp  &&  mv $temp $file
done

output:
Code:
;;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10
;;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10
;;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10

using var="1;3"
Code:
;31-JUL-10;;29-JUL-10;30-JUL-10;30-JUL-10
;31-JUL-10;;29-JUL-10;30-JUL-10;30-JUL-10
;31-JUL-10;;29-JUL-10;30-JUL-10;30-JUL-10

I actually not understood what alienated wants ? Smilie
I understand now so wanted thing Smilie

My method Smilie

Code:
#!/bin/bash
var=( "1" "2" )
val="[0-9]*-[A-Z]*-[0-9]*"
first=${var[0]}
end=$((${var[1]}-1))
sed "s/${val};/;/$first" file | sed "s/;${val};/;;/$end" > temp
      more temp

Code:
 
;;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10
;;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10
;;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10

when

Code:
 
var=( "1" "3" )

The output Smilie

Code:
;31-JUL-10;29-JUL-10;;30-JUL-10;30-JUL-10
;31-JUL-10;29-JUL-10;;30-JUL-10;30-JUL-10
;31-JUL-10;29-JUL-10;;30-JUL-10;30-JUL-10

# 9  
Old 08-01-2010
Portably avoiding the temp file shuffle with ed:
Code:
var=1\;2
IFS=\;
{ printf '1,$s/[^;]*;/;/%d\n' $var;  printf 'w\nq\n'; } | ed -s "$file"

or
Code:
var=1\;2
IFS=\;
ed -s "$file" <<EOED
$(printf '1,$s/[^;]*;/;/%d\n' $var)
w
q
EOED

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - add/edit to file and save - sed?

I'm working on a script to execute a number of items. One being, editing particular files to add certain lines. I'm attempting to utilize sed, but, having issues when running from a bash script. Assistance is greatly appreciated. My example: sed -i '14 i\ # add these lines add these lines to... (5 Replies)
Discussion started by: Nvizn
5 Replies

2. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

3. Shell Programming and Scripting

Using sed in a loop/to remove lines contained in variable from file

I've tried numerous commands, but I am not sure how to use sed in a loop. This is what I have: VARZ contains CARD_FILE_LIST and it also contains CARD_FILE_LIST2 so echo "$VARZ" CARD_FILE_LIST CARD_FILE_LIST2 I have a file with 60 lines in /tmp/testfile it and I want those lines deleted... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

Loop through file and replace with sed

Hello all, I need some help please. I got file1 with names. foo bar foo bar foo bar foo bar foo bar and I got file2 with some text some text some text #KEYWORD some text some text some text (3 Replies)
Discussion started by: stinkefisch
3 Replies

5. Shell Programming and Scripting

sed delete but save deleted output into other file

Hi guys, I am currently using this to save first 50 lines into top50.txt and delete them from list.txt ... it's 2 commands: head -n 50 list.txt > top50.txt && sed -i "1,50 d" list.txt I want to change that so it's 1 command - whereby sed removes the first 50 lines as above but that which is... (3 Replies)
Discussion started by: holyearth
3 Replies

6. Shell Programming and Scripting

loop through lines and save into separate files

I have two files: file-gene_families.txt that contains 30,000 rows of 30 columns. Column 1 is the ID column and contains the Col1 Col2 Col3 ... One gene-encoded CBPs ABC 111 ... One gene-encoded CBPs ABC 222 ... One gene-encoded CBPs ABC 212 ... Two gene encoded CBPs EFC... (7 Replies)
Discussion started by: yifangt
7 Replies

7. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

8. Shell Programming and Scripting

sed and save same file

hi, Im trying to do a sed and save it in teh same file using sed -i option But i think my system doesnt supports that option at all. BTW, im working in ksh. Is there any alternate for this? Thanks (3 Replies)
Discussion started by: dvah
3 Replies

9. Shell Programming and Scripting

what is the switch to let sed edit and save file

I remember there is a sed switch i can use to edit and save the file at the same time, but i cannot recall it at all. so instead of -> sed 's/A/B/' file > file-tmp -> mv file-tmp file what can i do to just let sed edit and save the "file" (4 Replies)
Discussion started by: fedora
4 Replies

10. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies
Login or Register to Ask a Question