LinuxQuestions.org
Visit Jeremy's Blog.
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 02-03-2011, 08:00 AM   #1
thamann
LQ Newbie
 
Registered: Jan 2011
Location: MICHIGAN
Posts: 5

Rep: Reputation: 0
How to show specific lines in a text file in Linux.


HI, I have created a text file in Linux, and I only want to show certain users. Here is my text file:
usr user tty Limbo?
11 12:06:13 APW no
12 12:06:13 APW no
13 12:06:13 BIW no
14 12:06:13 AIW no
15 12:06:13 WDOG no
16 20:25:17 prod-ui /dev/pts/14
17 00:31:26 prod-ui /dev/pts/26
18 08:29:12 prod-ui /dev/pts/28
19 22:54:17 prod-ui /dev/pts/19
20 12:07:29 mfg batch
21 05:12:27 prod-ui /dev/pts/21
22 05:59:44 prod-ui /dev/pts/0
23 21:31:25 prod-ui /dev/pts/12
24 05:20:27 prod-ui /dev/pts/11
25 19:29:21 prod-ui /dev/pts/15
26 06:01:19 EAGLE /dev/pts/3
27 21:31:35 prod-ui /dev/pts/17
28 06:01:29 EAGLE /dev/pts/16
29 06:05:30 prod-ui /dev/pts/9
30 05:29:30 EAGLE /dev/pts/23
31 06:05:33 prod-ui /dev/pts/31
32 07:59:54 prod-ui /dev/pts/25
33 06:15:13 EAGLE /dev/pts/20
34 02:30:43 prod-ui /dev/pts/10
35 05:29:59 prod-ui /dev/pts/24
36 06:05:57 prod-ui /dev/pts/18
I would only like to see lines that contain the word EAGLE.
TIA
T
 
Old 02-03-2011, 08:01 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You can use grep:
Code:
grep "EAGLE" infile
Hope this helps.
 
1 members found this post helpful.
Old 02-03-2011, 08:22 AM   #3
thamann
LQ Newbie
 
Registered: Jan 2011
Location: MICHIGAN
Posts: 5

Original Poster
Rep: Reputation: 0
Yes THAT DID it! THANKS.
Now it looks like this!!
qad-2010:/usr/local/bin # grep "EAGLE" users.txt
26 06:01:19 EAGLE /dev/pts/3
28 06:01:29 EAGLE /dev/pts/16
30 05:29:30 EAGLE /dev/pts/23
33 06:15:13 EAGLE /dev/pts/20


T
 
Old 02-03-2011, 08:23 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
Old 02-03-2011, 08:57 AM   #5
thamann
LQ Newbie
 
Registered: Jan 2011
Location: MICHIGAN
Posts: 5

Original Poster
Rep: Reputation: 0
Okay,,, I am still having a problem. this is my script:
--------------------------------------
proshut /qad/mfgprod -C list | awk '{print $1, $6, $8, $9}' > qadusers.txt;
EAGLE_users=`grep "EAGLE" qadusers.txt`
# Display list of EAGLE users
echo
echo "EAGLE Users:"
echo

echo $EAGLE_users;

echo
echo

exit;
--------------------------------------
But my Output is wrapped.
-------------------------------------
26 06:01:19 EAGLE /dev/pts/3 28 06:01:29 EAGLE /dev/pts/16 30 05:29:30 EAGLE /dev/pts/23 33 06:15:13 EAGLE /dev/pts/20 44 03:40:35 EAGLE /dev/pts/4 47 08:53:45 EAGLE /dev/pts/35 51 09:17:14 EAGLE /dev/pts/38 52 07:29:57 EAGLE /dev/pts/7 55 06:27:19 EAGLE /dev/pts/41 72 07:11:09 EAGLE /dev/pts/59 73 07:13:20 EAGLE /dev/pts/60 75 07:24:09 EAGLE /dev/pts/63 91 08:05:19 EAGLE /dev/pts/48 97 08:16:24 EAGLE /dev/pts/22 110 08:36:17 EAGLE /dev/pts/13
-----------------------------------------
How can I unwrap it??
 
Old 02-03-2011, 09:02 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
What about:

edit: nevermind

Last edited by sycamorex; 02-03-2011 at 09:57 AM. Reason: Writing without thinking:)
 
Old 02-03-2011, 09:12 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by thamann View Post
But my Output is wrapped.
This is the normal behaviour of the echo command. From the bash manual:
Code:
echo [-neE] [arg ...]
       Output  the  args, separated by spaces, followed by a newline.
Therefore multiple arguments separated by IFS (input field separator) that is space or tab or newline are printed out separated by spaces. If you embed the variable in double quotes, as shown by sycamorex above, the entire multi-line string is seen as a single argument and the echo command prints it out as is.

@sycamorex: maybe it is a typo, but why did you put a newline after the variable reference?
Code:
$ echo "$EAGLE_users\n"
26 06:01:19 EAGLE /dev/pts/3
28 06:01:29 EAGLE /dev/pts/16
30 05:29:30 EAGLE /dev/pts/23
33 06:15:13 EAGLE /dev/pts/20\n
As you can see, this output a literal \n at the end.
 
Old 02-03-2011, 09:26 AM   #8
thamann
LQ Newbie
 
Registered: Jan 2011
Location: MICHIGAN
Posts: 5

Original Poster
Rep: Reputation: 0
That did it!!
echo "$EAGLE_users"
55 06:27:19 EAGLE /dev/pts/41
72 07:11:09 EAGLE /dev/pts/59
75 07:24:09 EAGLE /dev/pts/63
91 08:05:19 EAGLE /dev/pts/48
110 08:36:17 EAGLE /dev/pts/13

It was the double quotes. It even works in my application

T

Wisdom is knowing when you can't be wise.
- Paul Engle
 
Old 02-03-2011, 09:56 AM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by colucix View Post
@sycamorex: maybe it is a typo, but why did you put a newline after the variable reference?
Code:
$ echo "$EAGLE_users\n"
26 06:01:19 EAGLE /dev/pts/3
28 06:01:29 EAGLE /dev/pts/16
30 05:29:30 EAGLE /dev/pts/23
33 06:15:13 EAGLE /dev/pts/20\n
As you can see, this output a literal \n at the end.
As embarrassed as I am, I'll have to admit this wasn't a typo. I don't know what I was thinking
Thanks for the correction.
 
Old 02-03-2011, 12:32 PM   #10
thamann
LQ Newbie
 
Registered: Jan 2011
Location: MICHIGAN
Posts: 5

Original Poster
Rep: Reputation: 0
What does the $ mean?
I put it here:
echo "$EAGLE_users"

but I also see in the example:
$ echo "$EAGLE_users\n"

inquiring minds want to know?? can you recommend a good book?

T
 
Old 02-03-2011, 01:05 PM   #11
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by thamann View Post
What does the $ mean?
I put it here:
echo "$EAGLE_users"

but I also see in the example:
$ echo "$EAGLE_users\n"

inquiring minds want to know?? can you recommend a good book?

T
By inserting $ in front of a variable name you return its value.

Have a look at this:
http://tldp.org/LDP/abs/html/
 
Old 02-03-2011, 02:54 PM   #12
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Quote:
Originally Posted by thamann View Post
What does the $ mean?
I put it here:
echo "$EAGLE_users"

but I also see in the example:
$ echo "$EAGLE_users\n"

inquiring minds want to know?? can you recommend a good book?

T

Well, you have already got the answer to your original question. So, I talk about this one which is already answered. But let me explain you smoe more in details:



In Shell Script (on Unix / Linux) when you set a variable's value you do not use a $ before the variable's name:

Code:
NUM=1
and you do not leave any space before and after the = sign.

When you want to use a variable's value then you simply prefix it with a $ sign:

Code:
echo $NUM
You can think of the $ sign as: Substitute whatever is contained by the variable sticking next to me.

If you put single quotes:

Code:
echo '$NUM'
then the output would be:

$NUM

and not 1.

Double quotes will do the substitution operation first before giving the output.

Code:
echo "$NUM"

Try this:

Code:
files=`ls -l`
echo -e "This is going to be a mess here...\n"
echo $files
Now try this:

Code:
files=`ls -l`
echo -e "This is going to be nice...\n"
echo "$files"
Note the backticks, they are not single quotes: `backticks`

Also note that -e with echo will interpret the escape character which is \n in the above example.

Sycamorex has suggest you an excellent guide: Advanced Bash Script.

You can also check Beginner's Bash Script guide on the same web site:

http://tldp.org

and there are PDF versions also.

Last edited by devUnix; 02-03-2011 at 02:55 PM.
 
Old 02-03-2011, 03:04 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by thamann View Post
but I also see in the example:
$ echo "$EAGLE_users\n"
If you refer to the $ in front of the command line, it is used in examples as the command prompt. By general agreement, the $ sign indicates a command run by normal users, whereas the # sign indicates commands that require root's privileges. Often I use the command prompt in my examples to distinguish between what you enter and the output of the command, e.g.
Code:
# ldconfig -p
1348 libs found in cache `/etc/ld.so.cache'
        libzypp.so.706 (libc6) => /usr/lib/libzypp.so.706
        libzvbi.so.0 (libc6) => /usr/lib/libzvbi.so.0
        libzvbi-chains.so.0 (libc6) => /usr/lib/libzvbi-chains.so.0
        libzip.so.1 (libc6) => /usr/lib/libzip.so.1
        libzio.so.0 (libc6) => /usr/lib/libzio.so.0
 
  


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
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 02:40 AM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 02:34 PM

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

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