Last active
July 9, 2016 17:18
-
-
Save chilismaug/497507becfcd60a735a716863f7c50d6 to your computer and use it in GitHub Desktop.
bash files loop example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for file in /etc/* | |
do | |
if [ "${file}" == "/etc/resolv.conf" ] | |
then | |
countNameservers=$(grep -c nameserver /etc/resolv.conf) | |
echo "Total ${countNameservers} nameservers defined in ${file}" | |
break | |
fi | |
done | |
//more stack-o answers for cat'ing file names and contents | |
//This should do the trick: | |
for filename in file1.txt file2.txt file3.txt; do | |
echo "$filename" | |
cat "$filename" | |
done > output.txt | |
//or to do this for all text files recursively: | |
find . -type f -name '*.txt' -print | while read filename; do | |
echo "$filename" | |
cat "$filename" | |
done > output.txt | |
//didn't work. I just wrote some really ugly awk code: | |
for i in $listoffiles do awk '{print FILENAME,$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' $i >> concat.txt done | |
//– Nick May 6 '11 at 22:13 | |
//...care to elaborate? That's about as simple as bash code gets. – Chris May 6 '11 at 22:14 | |
//@Nick: your awk line shouldn't even work, considering that $0 is the entire line, so you've actually got repeating columns in there... – Chris May 6 '11 at 22:20 | |
//@Chris You're so totally right. – Nick May 6 '11 at 22:26 | |
//@Nick: Nifty solution otherwise :) – Chris May 6 '11 at 22:27 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment