Solved

Shell scripting code needed

Posted on 2015-02-10
4
166 Views
Last Modified: 2015-02-16
Hi ,

I have a file 1.txt as below.

1.txt
file1,20
file2,30
file3,10

Need a shell script to check if the file1 is exist in folderA and has word count of 20 , likewise for other files.(eg.file2,30 and file3,10) .

Thanks
0
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 125 total points
ID: 40600535
I understood that

file1,20
file2,30
file3,10

is the content of 1.txt. Right?

FOLDER="folderA"
while read line
  do
    file=$FOLDER/$(cut -f1 -d"," <<<$line)
    count=$(cut -f2 -d"," <<<$line)
        if [[ -e $file ]] ; then
              if [[ $(wc -w < $file) -eq $count ]]; then
                  echo "File $file exists, word count $count OK"
                    else  echo "File $file exists, but word count is not OK"
               fi
          else echo "File $file does not exist"
       fi
  done < 1.txt
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 40600543
#!/bin/bash
cat <<EOF >1.txt
file1,20
file2,30
file3,10
EOF
IFS+=",";
cat 1.txt | while read f w ; do
 echo -n "$f "
 wc=`wc -w folderA/$f`
  if (( $w != ${wc% *}-0  )) ; then
    echo  ${wc% *} != $w
 else
     echo "ok"
 fi
done
0
 
LVL 23

Assisted Solution

by:savone
savone earned 125 total points
ID: 40600571
I find it interesting that different people have different ways to do things..


#!/bin/bash
cd folderA
for i in `cat 1.txt`; do
	if [ -e $i ]; then
		echo "File $i exists..."
		wanted=`echo $i | cut -d, -f 2`
		count=`cat $i | wc -l`
		if [ "$wanted" == "$count" ]; then
			echo "Word count matches"
		else
			echo "Word count doesn't match"
		fi
	else
		echo "File $i doesn't exists..."
	fi
done

Open in new window

0
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
ID: 40602036
Another solution

#!/bin/bash
while IFS="," read file count
do
  if [[ -f folderA/$file ]]
  then
     if [[ $(wc -w <folderA/$file) -eq $count ]]
     then
        echo "$file has matching word count"
     else
        echo "$file has mismatched word count."
     fi
  else
     echo "$file does not exist"
  fi
done <1.txt

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question