Formatting Diff output in Shell Script [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm currently using (diff -q directory1 directory2) to output the files in each directory that are different and printing them to a table in html.
Current output: "Files directory1/file1 and directory2/file2 differ"
What I want: "file1 has changed"
I do not want to use comm or sort the files because other applications are pulling from the files and are sensitive to ordering. Any idea on how to get this done?

you need to grep diff output for file that differ then use awk to print file name with your new format
diff -rq dir1 dir2 | grep "differ" | awk '{print $2 "has changed"}'

Will this work?
diff -q $file1 $file2 | awk '{print $2 " has changed"}'

Related

how can I remove some numbers at the end of line in a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I have a text file which contains a series of same line except at the end.
eg
lesi-1-1500-1
lesi-1-1500-2
lesi-1-1500-3
how can I remove the last number? it goes upto 250
to change in the file itself
sed -i 's/[0-9]\+$//' /path/to/file
or
sed 's/[0-9]\+$//' /path/to/file > /path/to/output
see example
You can do it with Awk by breaking it into fields.
echo "lesi-1-1500-2" > foo.txt
echo "lesi-1-1500-3" >> foo.txt
cat foo.txt | awk -F '-' '{print $1 "-" $2 "-" $3 }'
The -F switch allows us to set the delimiter which is -. Then we just print the first three fields with - for formatting.

Sort files in a directory by their text character length and copy to other directory [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to find the smallest file by character length inside of a directory and, once it is found, I want to rename it and copy it to another directory.
For example, I have two files in one directory ~/Files and these are cars.txt and rabbits.txt
Text in cars.txt:
I like red cars that are big.
Text in rabbits.txt:
I like rabbits.
So far I know how to get the character length of a single file with the command wc -m 'filename' but I don't know how to do it in all the files and sort them in order. I know rabbits.txt is smaller in character length, but how do I compare both of them?
You could sort the files by size, then select the name of the first one:
file=$(wc -m ~/Files/* 2>/dev/null | sort -n | head -n 1 | awk '{print $2}')
echo $file

shell script Key value compare 2 files [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Need help on shell script.
file1.txt
ABC1:10
ABC2:20
ABC3:15
file2.txt
ABC1:20
ABC2:10
ABC3:10
I have to compare the 2 files file1.txt and file2.txt .
when key matches from both files, We have to verify the value is greater or equal for that key compared to first file.
when ABC1 matches in 2 files it's value is compared. 10 (ABC1 in fil1.txt) is less than 20 (ABC2 in file2.txt) it shouldnt print, for second ABC2 20 is greater than 10 it has to print in output
when i run the shell script, it has to show below output.
ABC2:20
ABC3:15
I think you're looking for something like this?
#!/bin/bash
for line1 in $(cat $1)
do
key=$(echo $line1 | sed -E 's/(.+):.+/\1/')
val1=$(echo $line1 | sed -E 's/.+:(.+)/\1/')
for line2 in $(grep $key $2)
do
val2=$(echo $line2 | sed -E 's/.+:(.+)/\1/')
if (( $val2 <= $val1 ))
then
echo $line1
fi
done
done
If you save this as cmp.sh and make it executable then you can compare your files with
$ ./cmp.sh file1.txt file2.txt

How to create a Unix script to segregate data Line by Line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have some data in a MyFile.CSV file like this:
id,name,country
100,tom cruise,USA
101,Johnny depp,USA
102,John,India
What will be the shell script to take the above file as input and segregate the data in 2 different files as per the country?
I tried using the FOR loop and then using 2 IFs inside it but I am unable to do so. How to do it using awk?
For LINE in MyFile.CSV
Do
If grep "USA" $LINE >0 Then
$LINE >> Out_USA.csv
Else
$LINE >> Out_India.csv
Done
You can try with this
grep -R "USA" /path/to/file >> Out_USA.csv
grep -R "India" /path/to/file >> Out_India.csv
Many ways to do:
One way:
$ for i in `awk -F"," '{if(NR>1)print $3}' MyFile.csv|uniq|sort`;
do
echo $i;
egrep "${i}|country" MyFile.csv > Out_${i}.csv;
done
This assumes that the country name would not clash with other columns.
If it does, then you can fine tune that by adding additional regex.
For example, it country will be the last field, then you can add $ to the grep

Bash script manipulation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I work with Bash script and I want to get line from big text by special text
for example i have these lines
first fffffffffffffffffffffffffff
.................................
second ssssssssssssssssssssssssss
.................................
third ttttttttttttttttttttttttttt
and I want to get ssssssssssssssssssssssssss string .
Can anybody help me?
Is this what you want?
echo "$longstring" | awk '$1 == "second" { print $2 }'
since you seem to not have any criterion as to which line you want to output, i suggest something like:
echo "ssssssssssssssssssssssssss"
this is pretty robust regarding the content of your input, doesn't depend on a "file", and is a fast solution.
cat filename | grep "^second" | cut -d " " -f 2
Or, if you are ALF:
<filename grep "^second" | cut -d " " -f 2
Or
grep "^second" filename | cut -d " " -f 2

Resources