LinuxQuestions.org
Review your favorite Linux distribution.
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 01-05-2011, 09:23 AM   #1
abbotslad
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Rep: Reputation: 0
Post Scripting - reduce output from grep to first 8 characters


Hi there
I have file.txt - a database with 2 fields. Each line contains
1) a reference number (4 digit integer)
then a full-stop to seperate the 2 fields
2) A string name

eg

0234. abcsefset
0567. bcfsdfse

I am using a bash shell script to select each line in turn, and output the reference number:

Code:
#!/bin/sh

echo Starting...

record=0
cat file.txt | while read LINE ; 
do
	record=$((record+1))
	ref="$LINE"
	echo "$ref"
done
Which iterates through the database, but prints the whole line. I only want to print the first 4 digits, is there any way you can use the cut command to cut off the full stop and string on each line?
 
Old 01-05-2011, 09:28 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
of course there is. echo $ref | cut -c -4
 
Old 01-05-2011, 09:45 AM   #3
aggrishabh
Member
 
Registered: Nov 2010
Posts: 87

Rep: Reputation: 1
you can use awk command for this awkis a very powerful command.

awk -F '.' '{print $1}' file_name

Hope this will help.
 
Old 01-05-2011, 10:24 AM   #4
abbotslad
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks - one more quick question - if I want to store this output (4 digit number) as a variable, what syntax would I use?
 
Old 01-05-2011, 10:31 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You already have it one variable, but to store in another is just as easy:
Code:
ID=${LINE%.*}
 
Old 01-05-2011, 10:32 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by abbotslad View Post
Thanks - one more quick question - if I want to store this output (4 digit number) as a variable, what syntax would I use?
Command substitution. E.g.
Code:
variable=$(echo $ref | cut -c -4)
 
Old 01-05-2011, 10:35 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by grail View Post
You already have it one variable, but to store in another is just as easy:
Code:
ID=${LINE%.*}
Oops, I should have read the entire initial post. Of course, this is a more efficient solution than command substitution
@OP: Use grail's solution.
 
  


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
using grep to output non-matches into output file binny959 Linux - General 5 12-25-2010 12:20 AM
[SOLVED] Remove characters from grep output intz Programming 9 05-04-2010 07:03 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-31-2009 12:21 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM


All times are GMT -5. The time now is 02:33 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration