Solved

Shell scripting code needed

Posted on 2015-02-10
4
157 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now