Reputation: 49
I am writing a script that accepts someone's id, and the script should print this person's name! now the problem I have is that the name is unknown!
in the directory I am in right now I have a file and in this file there are lines and each line is in the following format :
id_number(from 6 digits) name(can be in any length and can include number at the end) number_of_brothers(positive number between 0-10) more_random numbers(from 6 digits too but this is optional which means it might not be any number in here )
the name can include a number at the end and also after the name, there must be a number that resembles the number of brothers, and after that, it is optional the line include numbers of id after the number of a brother ( whereas I said the number od is contained 6 digits) (also there could be spaces in the start of the line and at least one space between every two numbers or names) for example:
234218 Dan Simon 1 3 234122 234118 104134
(here the name i want to get is "Dan simon 1")
another example:
236501 chris brown singer 3
(the name I want to get here is "chris brown singer" )
my problem is how o get the name ! because the name could include a lot of words and also a number!
I started by getting rid of the extra spaces in the line and done grep to get the line that includes the correct id
#!/bin/bash
line = `grep ^$1 names_file`
correct_line=`echo $line`
Upvotes: 0
Views: 317
Reputation: 785481
Input file:
cat file
234218 Dan simon 1 3 234122 234118 104134
236501 chris brown singer 3
123456 John Right 2nd 5
Here is pure bash regex solution:
re='^[0-9]{6} +([a-zA-Z][a-zA-Z0-9 ]*) +[0-9]{1,2}( +[0-9]{6,})*$'
while IFS= read -r line; do [[ $line =~ $re ]] && echo "${BASH_REMATCH[1]}"; done < file
Dan simon 1
chris brown singer
John Right 2nd
Using gnu grep
:
grep -oP '^\d{6,}\s+\K[\w\s]+?(?=\s+\d{1,2}(?:\s+\d{6,})*$)' file
Dan simon 1
chris brown singer
John Right 2nd
Details:
^\d{6,}\s+
: Match 6+ digits followed by 1+ whitespace at the start\K
: Reset the match[\w\s]+?
: Match 1+ word or space characters(?=\s+\d{1,2}(?:\s+\d{6,})*$)
: Lookahead to ensure we have age number 1 or 2 digits aheadUpvotes: 1
Reputation: 247012
Here's how you could parse the file with awk
awk '{
# search backwards to find the first number between 0 and 10
for (i = NF; i > 2; i--) {
if (0 <= $i && $i <= 10) {
brothers_col = i
break
}
}
if (i == 2) next # did not find a name, skip this line
name = ""
for (i = 2; i < brothers_col; i++) {
name = name $i OFS
}
print name
' file
Upvotes: 0