Cut out the command including the space, which confuses the head command?

I am trying to use the output of the cut command in the head command to output a specific line. I believe that if the number fed to the parent team is 10+ works fine. So I'm wondering if cut -c1-2 includes a space for single digits that disable the head command?

my code

#!/bin/bash
echo "Enter your name"
read input
cut -c5-19 filelist | grep -n "$input" | cut -c1-2 > cat

while read cat
do
head -$cat filelist | tail -1 > filelist2
done < cat

      

Any advice or suggestions would be greatly appreciated! Thanks :) Edit

FULLRanjit Singh   Marketing  Eagles       Dean Johnson   
FULLKen Whillans   Marketing  Eagles       Karen Thompson 
PARTPeter RobertsonSales      Golden TigersRich Gardener  
CONTSandeep Jain   President  Wimps        Ken Whillans   
PARTJohn Thompson  Operations Hawks        Cher           
CONTCher           Operations Vegans       Karen Patel    
FULLJohn Jacobs    Sales      Hawks        Davinder Singh 
FULLDean Johnson   Finance    Vegans       Sandeep Jain   
PARTKaren Thompson EngineeringVegans       John Thompson  
FULLRich Gardener  IT         Golden TigersPeter Robertson
FULLKaren Patel    IT         Wimps        Ranjit Singh   

      

This is "file", The error I am getting is "head: invalid trailing option -:" If I find "Patel" as the name it works.

+3


source to share


2 answers


Answer to the revised question

Replace:

cut -c5-19 filelist | grep -n "$input" | cut -c1-2 > cat

      

FROM

cut -c5-19 filelist | grep -n "$input" | cut -d: -f1 >cat

      

grep -n

puts a colon between line number and line text. So it is natural to use a colon as the field separator for cut

and ask for a slice to return the first field.

From what you've shown, the script can be further simplified to:



read -p "Enter your name: " input
linenum=$(cut -c5-19 filelist | grep -n "$input" | cut -d: -f1)
head -$linenum filelist | tail -1 > filelist2

      

Answer to the original question

Since you did not show us filelist

, we can only guess about the problem. If you are correct about filelist

containing spaces, then this is the solution. Replace:

head -$cat filelist | tail -1 > filelist2

      

FROM

head -${cat## } filelist | tail -1 > filelist2

      

The construct ${cat## }

removes all leading spaces from varialbe cat

.

0


source


Some simplicity:

First, you can shorten echo ... read

by one read

using a parameter -p prompt

, for example:

read -r -p 'Enter your name: ' name

      

always use -r

, otherwise escape sequences are processed.

the second one, the remainder of the script, will do the following: print each line where the entered name is at positions 5 .. 19. This can be easily done with awk

, for example:

awk 'substr($0,5,15)~/pattern/'

      



where substr

returns from $0

(full input string), 15

characters (5-19 - 15 characters), starting at position 5

. ~/pattern/

matches the pattern. The name should be variable, eg. the complete command can be:

awk -v name=John 'substr($0,5,15)~name'

      

So, your script can be boiled down to the following two lines:

#!/bin/sh
read -r -p 'Enter your name: '
awk -v name="${REPLY}" 'substr($0,5,15)~name' file1 > file2

      

further, if you want a case insensitive match of the entered names, use

awk -v name="${REPLY}" 'tolower(substr($0,5,15))~tolower(name)'

      

0


source







All Articles