Solved

Shell scripting code needed

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

624 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