Sponsored Content
Top Forums Shell Programming and Scripting Sed save changes to same file in loop Post 302441565 by Scott on Sunday 1st of August 2010 05:48:02 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
LOGSAVE(8)						      System Manager's Manual							LOGSAVE(8)

NAME
logsave - save the output of a command in a logfile SYNOPSIS
logsave [ -asv ] logfile cmd_prog [ ... ] DESCRIPTION
The logsave program will execute cmd_prog with the specified argument(s), and save a copy of its output to logfile. If the containing directory for logfile does not exist, logsave will accumulate the output in memory until it can be written out. A copy of the output will also be written to standard output. If cmd_prog is a single hyphen ('-'), then instead of executing a program, logsave will take its input from standard input and save it in logfile logsave is useful for saving the output of initial boot scripts until the /var partition is mounted, so the output can be written to /var/log. OPTIONS
-a This option will cause the output to be appended to logfile, instead of replacing its current contents. -s This option will cause logsave to skip writing to the log file text which is bracketed with a control-A (ASCII 001 or Start of Header) and control-B (ASCII 002 or Start of Text). This allows progress bar information to be visible to the user on the console, while not being written to the log file. -v This option will make logsave to be more verbose in its output to the user. AUTHOR
Theodore Ts'o (tytso@mit.edu) SEE ALSO
fsck(8) E2fsprogs version 1.44.1 March 2018 LOGSAVE(8)
All times are GMT -4. The time now is 05:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy