Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Linux and Unix
Tag Cloud
access acer asus bios bsod computer crash driver drivers error ethernet excel freeze gaming gpu hard drive hardware hdmi internet laptop mac malware memory monitor motherboard music network printer problem ram registry router server slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
sed command

Reply  
Thread Tools
jacy's Avatar
Member with 120 posts.
 
Join Date: Jul 2004
Experience: Beginner
19-Apr-2005, 08:07 PM #1
sed command
Hi,
I need to output the long listing of files that were last modified in the month of february using the sed command from share directory. I can use other commands by piping it into sed. The problems is there were no files that were modified in february, so i have to display a message that there were no files that were modified in february. Am not familiar with the sed command. Please help. This is what i know

ls -l /share

this displays the list of all files and directories in share. I don't know how to perform an operation that checks if there were any files modified in the month of february and display a message.
tsunam's Avatar
Senior Member with 1,246 posts.
 
Join Date: Sep 2003
Experience: Linux~su
19-Apr-2005, 08:35 PM #2
you could actually do this a couple of ways. basically you need a if statement of the nature:

output=`ls -l /share | (find grep sed) (date area)`
if $output == (nothing)
then
echo "no files modified"
else
echo $output
fi

something of that nature...now this by no means is a working example but it gives you a idea of where to start and go. Squash and myself will help as you figure things out and get confused but we both like to see people learn and keep that knoledge for when they need it later (in linux you'd be surprised how often some bash/sed/grep/find/etc knowledge is helpful)
__________________
Gentoo Developer, and 64bit os user

"In feeding Mother Nature, you are fed in return" - Tsunam (2005). Concerning water conservation, and raising water tables.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
20-Apr-2005, 12:37 AM #3
I don't think you will need SED to accomplish this task. As much as I love SED, I find it a challenge to find ways not to use SED. Makes things more interesting. Before we get started, could you tell me what Operating system you are running and what shell you are using.
jacy's Avatar
Member with 120 posts.
 
Join Date: Jul 2004
Experience: Beginner
21-Apr-2005, 08:20 PM #4
sed
Thanks for ur help am in C shell working on Red hat linux. I used awk and it works. This is what i did
ls -l /share|awk '$6!= Feb {print "No files were modified in February"}

where 6 is the the sixth field which contains the month.

But the assignment calls for sed. This is what i did with sed.
ls -l| grep Feb|sed 's/Feb/February/'|wc -l

If there is Feb the it will be replaced by February and then that will be the input for wc -l which will count the number of lines for february if any. If i can store that number in a variable then i can use that in the awk statement which will accomplish the task. How can i declare a variable. Thanks for ur time.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
21-Apr-2005, 10:20 PM #5
Still not sure why you need to use sed. Why does the instructor want you to use SED. What do you mean by how do you declare a variable.

number=`ls -l| grep Feb|sed 's/Feb/February/'|wc -l`
echo $number

Is that what you want.
tsunam's Avatar
Senior Member with 1,246 posts.
 
Join Date: Sep 2003
Experience: Linux~su
21-Apr-2005, 10:42 PM #6
probably to get people to understand the usefulness of sed. Professors do strang things like that :P
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
21-Apr-2005, 10:49 PM #7
Quote:
Originally Posted by tsunam
probably to get people to understand the usefulness of sed. Professors do strang things like that :P
Yeah, I suppose. Everyone should know SED. But hey, lets make it a little more challenging then that.
tsunam's Avatar
Senior Member with 1,246 posts.
 
Join Date: Sep 2003
Experience: Linux~su
21-Apr-2005, 10:53 PM #8
LOL yes let's make them learn sed + regular expressions. Oh oh and toss escape characters on top.
jacy's Avatar
Member with 120 posts.
 
Join Date: Jul 2004
Experience: Beginner
22-Apr-2005, 12:36 AM #9
sed
Quote:
Originally Posted by Squashman
Still not sure why you need to use sed. Why does the instructor want you to use SED. What do you mean by how do you declare a variable.

number=`ls -l| grep Feb|sed 's/Feb/February/'|wc -l`
echo $number

Is that what you want.
My instructor wants us to use sed. We can use sed with other commands, but we have to use sed no matter what.

number=`ls -l| grep Feb|sed 's/Feb/February/'|wc -l`
here number is variable where the value will be stored, so i was asking how could i declare it b4 using it.

I tried the above command, it's giving me an error command not found.
Thanks for ur help.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
22-Apr-2005, 12:39 AM #10
I guess I don't understand what you mean by declare. Do you mean declare whay type of variable it is like you would in a programming language.
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
22-Apr-2005, 12:41 AM #11
Quote:
Originally Posted by jacy
I tried the above command, it's giving me an error command not found.
Thanks for ur help.
works fine on my box but I am using bash as my shell. Not sure if that makes a difference or not.
jacy's Avatar
Member with 120 posts.
 
Join Date: Jul 2004
Experience: Beginner
24-Apr-2005, 01:00 AM #12
sed
Quote:
Originally Posted by Squashman
I guess I don't understand what you mean by declare. Do you mean declare whay type of variable it is like you would in a programming language.
Exactly, that's what i meant. I looked in the share directory, there were no files modified in February, so the line count will be always zero. I came up with this

ls -l /home/faculty/share | sed '/Feb/p' | awk '$6!=0 {print "No files were modified in February"}'| uniq

This works for me.I have used sed, and am displaying the message, since there were no files modified in Feb. Thank u squashman for ur replies.

Last edited by jacy; 24-Apr-2005 at 01:54 AM..
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
24-Apr-2005, 01:09 AM #13
Quote:
Originally Posted by jacy
Exactly, that's what i meant.
You will have to look up the syntax for your Shell environment. I don't use the C shell but I know you can do it in a bash shell.
http://linuxreviews.org/beginner/abs.../en/x4854.html
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:07 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.