[Cygwin, sed] Using filenames as both files and search strings within sed
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[Cygwin, sed] Using filenames as both files and search strings within sed
I have a collection of documents within a folder, and I want to go through each of the files and then do the following:
1. Use the file as an input to sed.
2. Use the filename as a parameter in the sed command, with the extension stripped off.
So, lets assume my files are:
file1.txt
file2.txt
file3.sh
I can go through the files easily with the line code:
[pre]for file in `dir`; do echo $file;done[/pre]
I can extend that and strip the extension as well:
[pre]for file in `dir`; do echo $file to ${file%%.*}; done[/pre]
But how can I add these together? Let's say my requirement is as simple as this for sed:
Within the file file3.sh is the name of the document, followed by a version number. e.g. file3.sh contains somewhere within it the string "file3 Vxxx" where xxx is the version number. For each file I want to print this string and this string only from the file. There is text before and after this string.
I need to do this same task for every file in the current directory, but I cannot seem to get the sed line to work, nor can I seem to get sed to pick up the substituted filename (without the extension)
I can go through the files easily with the line code:
[pre]for file in `dir`; do echo $file;done[/pre]
Even easier is
Code:
for file in *; do echo "$file"; done
If you remember to quote $file, this will handle spaces in filenames as well.
Quote:
But how can I add these together? Let's say my requirement is as simple as this for sed:
Within the file file3.sh is the name of the document, followed by a version number. e.g. file3.sh contains somewhere within it the string "file3 Vxxx" where xxx is the version number. For each file I want to print this string and this string only from the file. There is text before and after this string.
You haven't written what you tried with sed, but I think grep -o will be the most convenient for this:
Code:
for file in *; do
grep -o "${file%%.*} V..." "$file"
done
Although that won't work so well if your filenames have special regex characters in them ("[", "*", "^", or "$").
Thanks very much, ntubski, your information really helped. I ended up doing this:
Quote:
for file in *; do grep -o --text "${file%%.*} V[0-9]\{3\}" "$file";done
Because I wanted to make sure the version number was really numeric and I had to force grep to treat the file as text, because it was coming up with "file3.sh matches" and not giving me the output - presumably because it thought the file was binary. But it works perfectly. Thanks for that. I think I need to check out grep in more detail.
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
To expand slightly on ntubski's post above, never use commands like ls or cat with for loops. See here for why:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.