0

I need to unzip a file and for each of the documents in that file, I need to make a correction to the comment (the files are written in C). I'm a bit confused as to how I can do this for each document in the file (I don't know the amount of documents, or their names), is there someway I can write a for loop across the folder to open each document individually and then edit them, within the shellscript?

I'm also a bit confused at the difference between sed and awk and which one I should be using here. I need to edit the comment (which begins with //) and believe I should be using awk but am not too sure.

My pseudocode for this (so far) would be as such

unzip $1
edit_comment(){
    cat file1 file2 ... filen > total.txt
    *some awk command that will search for all instances of // and replace the text on that line (after 
    the //) with a new comment*
}

Thanks!

1

1 Answer 1

0

I believe You are looking for How to do a recursive find/replace of a string with awk or sed.

Assumptions:
- The argument $1 is some folder.tar.gz or archive.zip or blah.bz4
- All files within that unzipped archive that contain comments should be changed

dir=$(echo $1 | sed 's#\..*##')
find $dir -type f -print0 | xargs -0 sed -i 's#//old comment #//new comment#'
Sign up to request clarification or add additional context in comments.

1 Comment

Yes sorry, your assumptions are right. I have a folder which is to be passed a command line parameter and needs to be unzipped and then change all the (C) comments in the files. I made some mistakes though, I actually do know all the documents names (and how many there are). The only other bit on information I missed out was that all of the files are written in C and need their comments changed (comments beginning with //) but one file is written in bash and needs it's comment change (which obviously begins with a #).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.