sed Command on Multiple Files and retaining the same file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed Command on Multiple Files and retaining the same file name
# 1  
Old 03-21-2016
sed Command on Multiple Files and retaining the same file name

Hi All

I am trying to run sed command to remove first 2 charcaters from a file on Multiple Files in my directory and what to retain the same file name .

I want to know how to retain the same file name but with changes .
Can some one please let me know how to proceed with this .

Below command just shows corrected output on command prompt , how to edit the file without changing the file name .

The below Script -

Code:
cd /home
ls * > test.txt
for i in `cat test.txt`;
  do
     sed '1s/..//' $i
   done

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Do not depend on moderators and administrators to edit your posts for you. (We would rather spend time suggesting ways to solve your problems than edit your posts.

Continued refusal to properly format your posts may result in you being banned from using this site.

Last edited by Don Cragun; 03-21-2016 at 05:38 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 03-21-2016
use sed -i

Code:
[user@host ~]$ cat file
hello
world
[user@host ~]$ sed '1s/..//' file
llo
world
[user@host ~]$ sed -i '1s/..//' file
[user@host ~]$ cat file
llo
world
[user@host ~]$

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-21-2016
sed -i is not working on AIX -

Code:
sed: Not a recognized flag: i

Any other way to fix this .

Last edited by Don Cragun; 03-21-2016 at 05:42 AM.. Reason: Add CODE and ICODE tags again.
# 4  
Old 03-21-2016
sed -i basically uses a tmp file and then renames it to the original file. You could do the same. So, send the output to a new temp file. Rename the tmp file to original file.

Code:
sed '1s/^..//' file > tmp; mv tmp file

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 03-21-2016
Thanks for your response -

It is not one file .
I am getting multiple files with date and time stamp

like abc.txt03212016124000, adc.txt03212016124001

How to fix this Scenario .

Last edited by Don Cragun; 03-21-2016 at 05:43 AM.. Reason: Add ICODE tags.
# 6  
Old 03-21-2016
You had it in your first post:
Code:
for i in *;
  do sed '1s/..//' $i > tmp && mv tmp $i
  done

# 7  
Old 03-21-2016
Untested, but should come close to what you want (although this seems extremely likely to destroy some data you might not want to destroy):
Code:
cd "$HOME"
for i in *
do	[ -f "$i" ] && sed '1s/..//' "$i" > "$i".mod && cp "$i".mod "$i" && rm -f "$i".mod
done

This is different from RudiC's suggestion and your original code as follows:
  1. the above assumes that you want to modify all of the regular files in your home directory,
  2. not all of the directories in the directory /home which seems to be what your script in post #1 in this thread would try to do, and
  3. not all files in the current directory as in RudiC's suggestion, AND,
  4. the combination of cp and rm commands (instead of just using an mv command) will preserve hard links on files (if there are any). Note, however, that if there are hard-linked files with more than one link in your home directory, two characters will be removed from the file for each link processed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

Curl command to download multiple files with a file prefix

I am using the below curl command to download a single file from client server and it is working as expected curl --ftp-ssl -k -u ${USER}:${PASSWD} ftp://${HOST}:${PORT}/path/to/${FILE} --output ${DEST}/${FILE} let say the client has 3 files hellofile.101, hellofile.102, hellofile.103 and I... (3 Replies)
Discussion started by: r@v!7*7@
3 Replies

3. Shell Programming and Scripting

Replace multiple patterns together with retaining the text in between

Hi Team I have the following text in one of the file j1738-abc-system_id(in.value1)-2838 G566-deF-system_id(in.value2)-7489 I want to remove system_id(...) combination completely The output should look like this j1738-abc-in.value1-2838 G566-deF-in.value2-7489 Any help is appreciated... (4 Replies)
Discussion started by: Thierry Henry
4 Replies

4. Shell Programming and Scripting

Splitting a file in to multiple files and passing each individual file to a command

I have an input file with contents like: MainFile.dat: 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 ... (4 Replies)
Discussion started by: rkrish
4 Replies

5. UNIX for Dummies Questions & Answers

Using sed command to remove multiple instances of repeating headers in one file?

Hi, I have catenated multiple output files (from a monte carlo run) into one big output file. Each individual file has it's own two line header. So when I catenate, there are multiple two line headers (of the same wording) within the big file. How do I use the sed command to search for the... (1 Reply)
Discussion started by: rebazon
1 Replies

6. Shell Programming and Scripting

SED command using multiple input files

What is the syntax to use multiple input files in a SED command. i.e. substitute a word with a phrase in every file in a directory. for every file in /usr/include that has the word "date" in the file grep -l '\<date\>' /usr/include/*.h find each occurrence of the word "time" in the file &... (3 Replies)
Discussion started by: sheoguey
3 Replies

7. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

8. Shell Programming and Scripting

handling multiple files using awk command and wants to get separate out file for each

hai all I am new to the world of shell scripting I wanted to extract two columns from multiple files say around 25 files and i wanted to get the separate outfile for each input file tired using the following command to extract two columns from 25 files awk... (2 Replies)
Discussion started by: hema dhevi
2 Replies

9. Shell Programming and Scripting

I need to find one command from multiple files and need to print that file which contains neede com

Hi all i need your help .. I am having a multiple file in directory and i have find out the Rcopy word from these files and need to print those files which contains the Rcopy word Thanks and regards Vijay sahu (2 Replies)
Discussion started by: vijays3
2 Replies

10. Shell Programming and Scripting

how to use multiple files in sed with w command

i have a command like : sed -n 's/^* /&/w even' <file if i want to write to multiple files like sed -n 's/^* /&/w zero two three' < file its not working it is taking "zero two three" as a single file i want to write to 3 seperate files . pls can anyone help me (2 Replies)
Discussion started by: santosh1234
2 Replies
Login or Register to Ask a Question