LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-18-2012, 02:57 AM   #1
lingh
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Rep: Reputation: Disabled
[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)

Can anyone offer some insight into this?
 
Old 10-18-2012, 03:20 AM   #2
ZackFair
LQ Newbie
 
Registered: Oct 2012
Location: Russia
Distribution: Arch, CentOS, RHEL, Debian, Ubuntu
Posts: 15

Rep: Reputation: Disabled
Not sure what you want to do with sed.
Also not sure I got your question right but here is my suggestion:

Code:
for filename in `ls`; do 
  name=`echo $filename|awk -F'.' {'print $1'}`
  cat $filename | grep "$name V"
done
If this is a standalone string (no extra text at beginning and at the end of the string) - this should do the trick.
 
Old 10-18-2012, 03:21 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by lingh View Post
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 "$").

Quote:
From man grep(1):
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
 
Old 10-18-2012, 04:32 AM   #4
lingh
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-20-2012, 10:30 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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:

Don't Read Lines With For


When operating on files, simple globbing is usually all you need. And when reading the contents of files or commands, use a while+read loop.
 
Old 10-20-2012, 10:38 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
( Deleted accidental double post )
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - Need help removing characyers from filenames kgoerbig Linux - Newbie 2 04-04-2012 12:37 PM
sed updated/touched also files that i didnt search replace in them tomerbd Linux - Newbie 2 10-15-2009 06:57 PM
How to search multiple files w/ SED then echo back the filenames and results??? hgate73 Programming 7 04-06-2009 03:11 PM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration