Solved

Shell scripting code needed

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux boot cd to do hardware report on PC? 3 64
powershell function location 6 49
Impact of disabling SMB v1 on Mac and Linux clients 4 555
number in printf 13 33
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…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

820 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