0

The file abc.txt is a line sequential file where each record is having multiple fields. I than want to grep two such fields from another line sequential file zzz.txt and display for comparison.

To do so, I am using a for loop i.e. for i in cat abc.txt. I than want to cut two different fields form the emerging string and want to grep these substrings from a file.

Example script

for i in `cat abc.txt`
do
field1=`cut -c10-15 $i`
field2=`cut -c25-30 $i`
grep $field1 zzz.txt
grep $field2 zzz.txt
done

Problem

When I try doing it the error message shows the string and says that

cut: <string in $i>: No such file or directory

found.

U880D
  • 8,601
  • 6
  • 24
  • 40
Titu 2910
  • 1
  • 1
  • 2
    And then? What's wrong with your attempt? What happens? What should happen? What's in `ABC.txt`? – Benjamin W. Jul 18 '19 at 15:58
  • ABC.txt is line sequential file with each record having multiple fields, I want to grep two such fields from another line sequential file zzz.txt and display for comparison.when I try doing it the error message shows the string and says that no such file found. – Titu 2910 Jul 19 '19 at 16:26
  • 1
    You should [edit] the question and add what you wrote in that comment. – Benjamin W. Jul 22 '19 at 14:44

1 Answers1

0

The error message means that cut command tries to use the content of the variable $i as filename and which is obviously not there.

To get your script working you would need to echo the variable content and pipe it into cut.

#! /bin/bash

for i in `cat abc.txt`
do

# echo variable content for debugging 
echo "i is ${i}"

field1=$(echo $i | cut -c10-15)
# echo variable content for debugging 
echo "field1 is ${field1}"

field2=$(echo $i | cut -c25-30)
# echo variable content for debugging 
echo "field2 is ${field2}"

grep ${field1} zzz.txt
grep ${field2} zzz.txt
done

Please have also a look into bash script use cut command at variable and store result at another variable or other similar questions here.

U880D
  • 8,601
  • 6
  • 24
  • 40