LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to show specific lines in a text file in Linux. (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-show-specific-lines-in-a-text-file-in-linux-860464/)

thamann 02-03-2011 08:00 AM

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

druuna 02-03-2011 08:01 AM

Hi,

You can use grep:
Code:

grep "EAGLE" infile
Hope this helps.

thamann 02-03-2011 08:22 AM

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

druuna 02-03-2011 08:23 AM

You're welcome :)

thamann 02-03-2011 08:57 AM

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??

sycamorex 02-03-2011 09:02 AM

What about:

edit: nevermind:)

colucix 02-03-2011 09:12 AM

Quote:

Originally Posted by thamann (Post 4247149)
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.

thamann 02-03-2011 09:26 AM

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

sycamorex 02-03-2011 09:56 AM

Quote:

Originally Posted by colucix (Post 4247172)
@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.

thamann 02-03-2011 12:32 PM

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

sycamorex 02-03-2011 01:05 PM

Quote:

Originally Posted by thamann (Post 4247362)
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/

devUnix 02-03-2011 02:54 PM

Quote:

Originally Posted by thamann (Post 4247362)
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.

colucix 02-03-2011 03:04 PM

Quote:

Originally Posted by thamann (Post 4247362)
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



All times are GMT -5. The time now is 09:28 AM.