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
# 1  
Old 08-01-2010
Sed save changes to same file in loop

I have got problems saving sed changes to the same file in a loop.
Basically I want the delimited value in every line of the file to be set to blank according to the value stored in var. var can be changed anytime.
I do not have sed -i and i've tried to mv the file. Any other ideas?

My file contents
Code:
31-JUL-10;31-JUL-10;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10
31-JUL-10;31-JUL-10;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10
31-JUL-10;31-JUL-10;29-JUL-10;29-JUL-10;30-JUL-10;30-JUL-10

to
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

My code
Code:
var="1;2"
IFS=";"

    for i in $var
    do
        sed 's/[^;][^;]*;/;/$i' $file> $temp 
        mv $temp $file
    done

# 2  
Old 08-01-2010
Hi.

You need double quotes around the sed, otherwise the shell won't expand $i.

Also, after deleting each field, you need to subtract 1 from other field numbers (what was field 2, after deleting field 1, will now be the new field 1).

And for it to work on the last field, you need either to modify the sed, or add a semi-colon to the end of the line:

Code:
$ cat sedTest
file=file1
temp=file1.out

var="1;6"
IFS=";"

X=0
for i in $var
do
  I=$((i - X))
  sed "s/$/;/;s/[^;][^;]*;/;/$I;s/;$//" $file> $temp 
  X=$((X + 1))
  mv $temp $file
done

cat $file

$ ./sedTest
;31-JUL-10;29-JUL-10;29-JUL-10;30-JUL-10;
;31-JUL-10;29-JUL-10;29-JUL-10;30-JUL-10;
;31-JUL-10;29-JUL-10;29-JUL-10;30-JUL-10;

$ # with var="1;2"

$ ./sedTest
$ ./sedTest
;;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

This User Gave Thanks to Scott For This Post:
# 3  
Old 08-01-2010
you can try more simple Smilie

Code:
# justdoit
while read line
    do
     echo "$line" | sed 's/[^;]*;[0-9]*-[A-Z]*-[0-9]*;/;;/' >> temp
    done <file
     mv temp file ; more file

Code:
./justdoit
;;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

# 4  
Old 08-01-2010
Quote:
Originally Posted by ygemici
you can try more simple Smilie

Code:
# justdoit
while read line
    do
     echo "$line" | sed 's/[^;]*;[0-9]*-[A-Z]*-[0-9]*;/;;/' >> temp
    done <file
     mv temp file ; more file

Code:
./justdoit
;;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

I guess if the idea was to remove the first two fields, this would do:

Code:
$ sed -i.bak "s/[^;]*;[^;]*/;/" file
# or
$ sed "s/[^;]*;[^;]*/;/" file > temp && mv temp file

But I think the O/P wanted more flexibility than that.
# 5  
Old 08-01-2010
yes, to clarify, i need the variable to store the position of the place that i need to be blank, not just the first 2 fields, of which scottn's example works beautifully. thanks.
# 6  
Old 08-01-2010
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

# 7  
Old 08-01-2010
Another one with awk:
Code:
awk -v var="1;3" 'BEGIN{FS=OFS=";";split(var,a)} 
{
  for(i in a)$(a[i])=""
}
1' file > temp  &&  mv temp file


Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors
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