LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2008, 06:46 AM   #1
haliban
LQ Newbie
 
Registered: Mar 2006
Posts: 9

Rep: Reputation: 0
Smile bash question about sed '"$i"!d' listofusers


Hello All,

Here it is a part of my script.

Code:
#!/bin/bash

users=/web/users
max=`/bin/cut -f1 users | wc -l`


        for ((i=1; i <= $max ; i++))
        do
         curusr= sed '"$i"!d' users
                                              
 done

I have list of users in file plain text file named "users" and on each line there is just one username.

- with
Code:
 max=`/bin/cut -f1 users | wc -l`
I am getting the whole number of lines/users of the file and use it in "for" loop.

- When I try to
Code:
curusr= sed '"$i"!d' users
get actual username from each line I get an error message :


Code:
+ sed '"$i"!d' users
sed: -e expression #1, char 1: unknown command: `"'
When I execute
Code:
 sed '2!d' users
from the console I get the username from the second line.

I assume there is something wrong in my syntax. I look around the Internet and found a lot of interesting topics but not how to fix it.


Any help how to make it working ?

Thank you for your help!
 
Old 09-21-2008, 06:51 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
how does your input file look like and what is your final output?
 
Old 09-21-2008, 07:56 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
It looks like you are using addressing, which has the following syntax:

sed '<line#><action>'
OR
sed '/<pattern>/<action>'

Also, the quoting needs to be changed: single-quotes protect everything from BASH, so, the double-quote is being sent to SED.

I think you need:
sed "/$i/!d"

But, why the negation? The same result can be gotten this way:
sed -n "/$1/p"

Excellent SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 09-24-2008, 02:01 AM   #4
haliban
LQ Newbie
 
Registered: Mar 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you for your help guys ! :-)

pixellany, I have read in SED tutorial that there are 3 different ways to print specific line from text file and chose
sed "/$i/!d"

I put in my script sed -n "/$1/p" as you advised.

There is something very interesting I have noticed, when I have
Code:
sed -n "/1/p" users
inside the script it shows me the user name located on the first line, but if I do
Code:
sed -n "/3/p" users
It does not show me anything (no error message). On the other hand if I do
Code:
sed -n '3 p' users
from shell it gives me the user name on line 3 :-) Funny....

The file with users looks like :

Code:
[myuser@theserver webusers]$ cat users 
user1
user2
user3
user4
...

...
...
user35
I feel that there is something small and silly I am missing .... do not know !

Sed tutorial is very valuable, thanks again ;-)
 
Old 09-24-2008, 02:12 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I get the correct results:
Code:
$ sed -n "/3/p" users
user3
$ sed -n "3p" users  
user3
Take note to what Pixellany indicated about the differences between an input line number and a line that contains a pattern.
 
Old 09-25-2008, 03:14 AM   #6
nitin_nitt
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Rep: Reputation: 0
Try this:

Code:
for i in `cut -f 1 /web/users`; do curuser=echo $i; done
If you don't want to change the code for some reason, change the sed command as:

Code:
sed -n $i'p' users

Last edited by nitin_nitt; 09-25-2008 at 03:26 AM.
 
Old 09-25-2008, 06:42 PM   #7
haliban
LQ Newbie
 
Registered: Mar 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you very much for your help !

All good and working so far ;-)
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
bash Problem with "sed" command blubbfish Linux - Newbie 3 06-01-2004 10:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 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