Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell scripting code needed

Posted on 2015-02-10
4
Medium Priority
?
170 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 500 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 85

Assisted Solution

by:ozo
ozo earned 500 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 500 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 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

886 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