LinuxQuestions.org
Help answer threads with 0 replies.
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 10-23-2004, 02:43 PM   #1
JordanH
Member
 
Registered: Oct 2003
Location: Toronto, Canada
Distribution: Ubuntu, FC3, RHEL 3-4 AS Retired: SuSE 9.1 Pro, RedHat 6-9, FC1-2
Posts: 360

Rep: Reputation: 30
BASH: First words in a line


Hi All,

I have a log file where each line is a thousands (?) of characters long. I want to be able to display this file in a console so only the first word or words or X number of characters are shown. I want to ignore the rest.

(Specifically, it is a log that begins with date and IP address)

I'm stumped trying to find a command that will only display the first few characters or words of each line.

Any thoughts?
J.
 
Old 10-23-2004, 02:50 PM   #2
bushidozen
Member
 
Registered: Oct 2004
Posts: 215

Rep: Reputation: 30
I personally do not know of a bash command to view the first few lines of a file, but try this:

tac <filename> (Reverses a file - last line first and so on)
tail <filename> (Displays the last few lines of a file)

Doing both of these things might give you the results you want.
 
Old 10-23-2004, 03:04 PM   #3
JordanH
Member
 
Registered: Oct 2003
Location: Toronto, Canada
Distribution: Ubuntu, FC3, RHEL 3-4 AS Retired: SuSE 9.1 Pro, RedHat 6-9, FC1-2
Posts: 360

Original Poster
Rep: Reputation: 30
I appreciate you response but I'm not looking for the first few lines, I'm looking for every line but only the beginning of the line.

In case you were wondering, you can view the first few lines of a file with these commands
$ head
or
$ cat foo.txt | less

This works.... but is ugly. Someone please give me some suggestions
Quote:
#!/bin/bash
while read LINE
do
Y=0
for WORD in $LINE
if [ $Y = 0 ]
then
echo $WORD
fi
Y=$((Y+1))
done
done < data.txt
 
Old 10-23-2004, 05:30 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Whenever I encounter input files that have more or less unbounded line lengths, I tend to write a custom C program to parse it. In this case a very simple C program will do the job. I would use fgets() and only specify an input buffer size of 100, print that to stdout then do a fgets() loop until the end of line char is read while discarding the rest of the line.
You can then do /usr/local/bin/mycustom inputfile.log | awk { or whatever }
to trim down the lines to a more managable size.
If there is a built in shell command already that can do this, I'm not aware yet.
 
Old 10-24-2004, 01:32 AM   #5
JordanH
Member
 
Registered: Oct 2003
Location: Toronto, Canada
Distribution: Ubuntu, FC3, RHEL 3-4 AS Retired: SuSE 9.1 Pro, RedHat 6-9, FC1-2
Posts: 360

Original Poster
Rep: Reputation: 30
Thanks for the suggestion. It's been a while since I used C so it was faster to modify my example above to pull out the 1st and 4th keyword.

I guess we can consider my question answered if there isn't a command.
Cheers.
 
Old 10-24-2004, 02:05 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
The cut command can be used for this:
Code:
itsme@itsme:~/C++$ cat file.txt
This is a line
Another line again
itsme@itsme:~/C++$ cut --delimiter=' ' -f 1 < file.txt
This
Another
itsme@itsme:~/C++$
 
1 members found this post helpful.
Old 10-24-2004, 02:14 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Awk would be good to use for this also. You can output the first, third and fourth columns if you wished, and add a heading line. You would also be able to pipe the output to a pager or a file and/or write certain columns to a file.
 
Old 10-24-2004, 10:00 AM   #8
JordanH
Member
 
Registered: Oct 2003
Location: Toronto, Canada
Distribution: Ubuntu, FC3, RHEL 3-4 AS Retired: SuSE 9.1 Pro, RedHat 6-9, FC1-2
Posts: 360

Original Poster
Rep: Reputation: 30
awk was the magic word.

Quote:
#/!bin/bash
LOG="/var/log/httpd/access_$1"
cat $LOG | grep $2 | awk '{print $4 " " $1}'
Excellent.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
[bash] Put words from file to array mispunt Programming 4 11-04-2004 10:53 AM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM
Giving multiple words as argument for bash ivanatora Linux - General 5 01-07-2004 02:04 AM


All times are GMT -5. The time now is 01:27 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration