Combine file names and content - linux

I got several files like this:
First file is named XXX
1
2
3
Second file is named YYY
4
5
6
I would like to write content and the file names to a separate file that would look like this:
1 XXX
2 XXX
3 XXX
4 YYY
5 YYY
6 YYY
Can someone suggest a way to do this?

awk '{print $0,FILENAME}' file1 file2
Or Ruby(1.9+)
$ ruby -ne 'puts "#{$_.chomp} #{ARGF.filename}"' file1 file2

Without further explanation of what you actually need this should work:
for file in $(ls)
do
echo -n $file >> outfile
cat $file >> outfile
done

Related

grep matches counted per keyword?

I am using
$ grep -cf keyword_file Folder1/*
to generate a count of how many lines, in files housed within Folder1, match a keyword from the keyword_file.
this generates an aggregate count total such as
file1: 7
file2: 4
file3: 9
my problem:
I would like the output to be in the form below
first_keyword
file1: 5
file2: 0
file3: 5
second_keyword
file1: 0
file2: 3
file3: 1
third_keyword
file1: 2
file2: 1
file3: 3
so that I can see how many times each individual keyword was present in a line of each file.
How do I achieve this?
===== added detail ====
keyword_file is at Documents/script_pad/keyword_file
Folder1 is at Documents/script_pad/Folder1
what worked for me was
creating a file "Documents/script_pad/loop2" containing
#!/bin/bash
cat Documents/script_pad/keyword_file | while read line
do
echo $line; grep -c $line Documents/script_pad/Folder1/*
done
which when run resulted in
$ bash Documents/script_pad/loop2
first_keyword
Documents/script_pad/Folder1/file1:5
Documents/script_pad/Folder1/file2:0
Documents/script_pad/Folder1/file3:5
second_keyword
Documents/script_pad/Folder1/file1:0
Documents/script_pad/Folder1/file2:3
Documents/script_pad/Folder1/file3:1
third_keyword
Documents/script_pad/Folder1/file1:2
Documents/script_pad/Folder1/file2:1
Documents/script_pad/Folder1/file3:3

How can I combine one file's tail with another's head?

I know how to take e.g the first 2 lines from a .txt data and appending it to the end of a .txt data. But how should I add the last 2 lines of a .txt data to the 1st line of a .txt data
I've tried :
tail -n 2 test1.txt >> head test1.txt # takes last 2 lines of text and adding
it to the head
Looks awfully wrong but I can't find the answer anywhere, doing it with tail and head.
tail n 2 test1.txt >> head test1.txt
cat test1.txt
Someone please correct my code so I get my expected result.
Just run the two commands one after the other -- the stdout resulting from doing so will be exactly the same as what you'd get by concatenating their output together, without needing to do an explicit/extra concatenation step:
tail -n 2 test1.txt
head -n 1 test1.txt
If you want to redirect their output together, put them in a brace group:
{
tail -n 2 test1.txt
head -n 1 test1.txt
} >out.txt
What about:
$ cat file1.txt
file 1 line 1
file 1 line 2
file 1 line 3
file 1 line 4
$ cat file2.txt
file 2 line 1
file 2 line 2
file 2 line 3
file 2 line 4
$ tail -n 2 file1.txt > output.txt
$ head -n 1 file2.txt >> output.txt
$ cat output.txt
file 1 line 3
file 1 line 4
file 2 line 1

Cat headers and renaming a column header using awk?

I've got an input file (input.txt) like this:
name value1 value2
A 3 1
B 7 4
C 2 9
E 5 2
And another file with a list of names (names.txt) like so:
B
C
Using grep -f, I can get all the lines with names "B" and "C"
grep -wFf names.txt input.txt
to get
B 7 4
C 2 9
However, I want to keep the header at the top of the output file, and also rename the column name "name" with "ID". And using grep, to keep the rows with names B and C, the output should be:
**ID** value1 value2
B 7 4
C 2 9
I'm thinking awk should be able to accomplish this, but being new to awk I'm not sure how to approach this. Help appreciated!
While it is certainly possible to do this in awk, the fastest way to solve your actual problem is to simply prepend the header you want in front of the grep output.
echo **ID** value1 value2 > Output.txt && grep -wFf names.txt input.txt >> Output.txt
Update Since the OP has multiple files, we can modify the above line to pull the first line out of the input file instead.
head -n 1 input.txt | sed 's/name/ID/' > Output.txt && grep -wFf names.txt input.txt >> Output.txt
Here is how to do it with awk
awk 'FNR==NR {a[$1];next} FNR==1 {$1="ID";print} {for (i in a) if ($1==i) print}' name input
ID value1 value2
B 7 4
C 2 9
Store the names in an array a
Then test filed #1 if it contains data in array a

How to sort data while printing line by line in Bash

I'm writing a shell script traversing a list of directories and counting words from files inside them. The code prints data each time I read a file. So the output is not sorted. How can I sort it?
The output right now is like this:
cat 5
door 1
bird 3
dog 1
and I want to sort it first by second column and then by first column:
dog 1
door 1
bird 3
cat 5
You can pipe your shell script to:
sort -n -k2 -k1
With -n you specify numeric sort and with -k2 that you want to sort first by the second field and with -k1 to sort then by first field.
First of all, I tried to reproduce what OP is doing, so after creating the files, I tried this command:
% for i in *; do echo -n "$i "; wc -w < $i; done
bird 3
cat 5
dog 1
door 1
Then I have added the sort:
% (for i in *; do echo -n "$i "; wc -w < $i; done) | sort -n -k 2 -k 1
dog 1
door 1
bird 3
cat 5

combining the text files in to one text file

i have a requirement like the following.
i am using linux
i have a set of text files like text1.txt ,text2.txt, text3.txt.
now i am combining into one final text file.
text1.txt
1
NULL
NULL
4
text2.txt
1
2
NULL
4
text3.txt
a
b
c
d
i am using the following command :
paste -d ' ' text1.txt text2.txt text3.txt >> text4.txt
i am getting the :
text4.txt
1 1 a
2 b
c
4 4 d
but i want the output like the following
text4.txt
1 1 a
NULL 2 b
NULL NULL c
4 4 d
NOTE :- NULL means space
i am passing this text4 to another loop as a input so here there i am reading the variable by positionl
thanks in advance
I expect that you want TABs separating your records in file4.txt... what about this?
NLINES=$(wc -l file1.txt | awk '{print $1}')
rm -f file4.txt
for i in $(seq 1 $NLINES); do
rec1=$(sed -n "$i p" file1.txt)
rec2=$(sed -n "$i p" file2.txt)
rec3=$(sed -n "$i p" file3.txt)
echo -e "$rec1\t$rec2\t$rec3" >> file4.txt
done
But actually paste, without "-d ' '" gave the same exact result!
you can achieve same with AWK command
awk '{a[FNR]=a[FNR]$0" "}END{for(i=1;i<=length(a);i++)print a[i]}' text1.txt text2.txt text3.txt >> text4.txt

Resources