The first three lines are headers and the rest of the lines contain client id number followed by their statistical data.
For example,
line 1: 1 Header
line 2: 2 Header
line 3: 3 Header
line 4: 0801 9 10 2 1
line 5: 0802 7 11 4 6
.
line 18: 0890 2 6 9 12
.
.
line 30: 0930 14 8 4 9
I want to write a command string that will only display the first 3 lines and line 18.
This is what I have tried so far and it doesn't work:
head -3 statistics.dat | grep '0890'
I've also tried:
cat statistics.dat | head -3 | grep '0890'
I have tried lots of variations on the commands listed above but nothing seems to work. Can someone help me with this?
Am I using the right commands or are their other commands that I should be using?
When you do:
head -3 statistics.dat | grep '0890'
this selects the first 3 lines and does not display them, but instead forwards them (pipes) into the next command for processing, and of course the grep will not find that string in that set of lines.
If those header lines were very easy to identify, a single grep command with two search arguments could do it, such as:
grep -e Header -e "^0890" statistics.dat
If not, do two separate commands:
head -3 statistics.dat
grep "^0890" statistics.dat
The carat is anchoring its search to the very beginning of each line (which I think is correct for your data) so that it will not accidentally find the search string somewhere else in the line. Here is a script that you can execute with a parameter:
Code:
#!/bin/sh
client=${1?Need one parameter - the client ID}
head -3 statistics.dat
grep "^$client" statistics.dat
exit 0
Thanks Jimbo! for your help I now understand what the pipe "|" does. I was thinking that it was just used to separate the commands on the same line.I have to learn that there is more than just one way to write a command string that does the same thing.
I just got to carried away with that head command.
Great! unix probably just got a lot more fun for you. Speaking of separators, the semicolon separates multiple commands on the same line. And || and && are command separators like semicolon, but conditional. The former means do 2nd command only if the 1st has an error, and the latter means do 2nd command only if the 1st has no error. They are not pipes - output is not forwarded to the 2nd command.
Code:
badcommand || echo 'not a good command'
pwd && echo 'is where I currently am'
Hi Folks,
I have a file name abc.xml in my windows machine at the location c:\ytr\abc.xml
which I want to place at the unix box machine inside cde directory.. at the following location that is /opt/app/cde/
now the credentials of unix box are
abc345 -->(dummyid)
ftyiu88--->(dummy passwd)
... (4 Replies)
I'm trying to write a bash script called YN that looks like the following
YN "Specify a question" "doThis" "doThat"
where "doThis" will be executed if the answer is "y", otherwise "doThat".
For example
YN "Do you want to list the file dog?" "ls -al dog" ""
Here's my attempt... (3 Replies)
Hi all,
I wish to know whether Unix can access window's file in Unix's terminal?
Apart from that, how to copy files or share files between Window and Unix? I get to know of secure copy, however, my company's Unix does not support the feature of secure copy? Any other method for me to share/... (5 Replies)
I am going through the Unix Made Easy second edition book by John Muster. So far it's been very informative and I can tell it may be a bit out of date.
In one of the exercises it talks about the "sort" command and using it to sort column's of data etc. The "sort" command has changed a bit and... (1 Reply)
Hi,
Well my title isn't very clear I think. So to understand my goal:
I have a script "test1"
#!/bin/bash
xvkbd -text blabla
with xbindkeys, I bind F5 key in order it runs my test1 script
So when I press F5, test1 runs.
I'm under Emacs/Vi and I press F5 in order to have "blabla" be... (0 Replies)
Hi ,
I have a piece of code ...wherein I need to assign the following ...
1) A command line argument to a variable
e.g origCount=ARGV
2) A unix command to a variable
e.g result=`wc -l testFile.txt`
in my awk shell script
When I do this :
print "origCount" origCount --> I get the... (0 Replies)
Hello,
I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is
sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM"
sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM"
sh "nlshCMD 'STREAM <job name3>' |... (1 Reply)
Hello,
I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is
sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM"
sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM"
sh "nlshCMD 'STREAM <job name3>' |... (0 Replies)
hi all. thanks for looking
i am doing some homework.
one question is that when type
wc
and then how to tell the program that we have finished entering data?
also
why do some operating systems report 22 as the number of bytes in the file above, while others only 20?
thanks so much,... (1 Reply)