using Grep for only a certain number of columns


 
Thread Tools Search this Thread
# 1  
using Grep for only a certain number of columns

I am given a file with a list of names for the first 20 characters. The 20 characters after is a list of names that somebody is supposed to team up with.

Now i'm trying to write a simple script that will read a name somebody inputs and then looks only in the first 20 characters.

For example. (Due to the nature of BBS posting length of columns are not given, the first name is the desired target for searching, the second name is the person teamed up with the first.

John Bow : Lucy Hemmings
Bob Billy : Bibliotheque Frenchness
Lucy Hemmings : John Bow

if somebody inputs John, I would get both Lucy and John, and this is not what I want. Any ideas?

I am considering piping grep -w with something else as it seems to at least cut out all the names which have no relevance.
# 2  
Quote:
Originally Posted by MaestroRage
I am considering piping grep -w with something else as it seems to at least cut out all the names which have no relevance.
The extra whitespace in that sample is sort of hard to write something exacting. But yeah grep and pipe are the solutions imo.

grep -i -E "^$NAME " $FILE | cut -f2 -d:
# 3  
Code:
in_file=/path/to/file
read input
awk -v name=$input -F: 'tolower($1) ~ name { print $2 }' $in_file

Is this what you're looking for ?
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #89
Difficulty: Easy
Software management applications like the Ubuntu Software Center are front-ends to the core terminal commands use to install and manage software applications.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Number of columns

Hi All I am using the below code to check the number of columns in a delimited file are same as expected. Code is give below awk -F '|' -v line=$2 'NF!=line {print NF,$0}' file Here the challenge is that , delimiter is different for each file, it can be pipe, comma, or semi colon.I want to... (4 Replies)
Discussion started by: ginrkf
4 Replies

3. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

4. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

5. Shell Programming and Scripting

How to check the number of columns in a line??

hi, i have a file with many records and each record may or may not have 6 columns. for example file1 : first second third fourth fifth sixth first second third fourth fifth first second third fourth fifth sixth first second third fourth fifth sixth seventh eigth if i cat the file and... (21 Replies)
Discussion started by: Little
21 Replies

6. Shell Programming and Scripting

Number of columns in xml file

How to find the number of columns in xml file. i tried following command. #!bin/ksh cat $1 | grep -c "</mdm:attribute>" exit 0 but i am not getting accurate result which is not matching with the manual count. data format : <mdm:attribute> value </mdm:attribute> (6 Replies)
Discussion started by: victory
6 Replies

7. Shell Programming and Scripting

Delete rows with particular number of columns

Hello Guys I have a flat file with few thousands of rows. Now each rows have different number of columns I want to delete the rows which has not equal to 749 columns Can you guys please let me know how to do the same Your help is much appreciated. (2 Replies)
Discussion started by: Pratik4891
2 Replies

8. Shell Programming and Scripting

How to subtract a number from all columns?

Hi, I want to subtract a number from all columns except the first column. I have a number of files each having different columns around 60/70. How to do that in awk or any other command? Thanks Input Col 1 Col 2 Col3 - - - - Col55 1 .0123 .098 - - - 0.6728 2 - -... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

9. Shell Programming and Scripting

concatenate 'n' number of columns in a file

i have a file which may have 'n' number of columns 1 222 fafda 32 afdaf 4343 4343 234 43fdaf 4343 fdd fdfd fdfd fdd fdfd fdfd fdfd fdfd fdfd fdd fdfd fdfd need to concatenate the columns with... (3 Replies)
Discussion started by: mlpathir
3 Replies

10. Shell Programming and Scripting

number subtraction of multiple columns

I get the point of number subtraction in one column awk 'NR==1 {n=$1; next}; {n-=$1} END {print n}' inputfile but I cannot figure it out how to do this to multiple columns. awkward. (6 Replies)
Discussion started by: awkward
6 Replies

Featured Tech Videos