Reflecting the sort of one file in another - linux

I have two files, say f1 and f2.
f1 has a list of items that can't be compared (they are all alpha numeric, each on its own line). It's companion file f2 has a list of items that can be compared each on its own line.
I have sorted f2 in reverse order to produce a file f3. I want to reflect this in f1 to produce a file f4.
Example:
f1:
Dan
Sam
James
f2:
3
1
2
f3 (which is a reverse sort of f2):
3
2
1
I want f4 to be:
Dan
James
Sam
I hope this example illustrates what I'm trying to achieve.

Here's a quick and dirty way using the paste command. It should work if your files are simple.
% cat numbers.txt
3
1
2
% cat names.txt
Dan
Sam
James
% paste numbers.txt names.txt | sort -nr | awk '-F\t' '{print $2}'
Dan
James
Sam

Related

Can you write an IF statement based on a character in a string?

In excel I have a column that contains given names. Some of those are one word and some are two words. Think of something like this:
FIRST NAME
Emma
Anthony
Anne Marie
John
I want to concatenate this column with another one to create an id, and I am just interested in getting the first word. So my ideal output would look like column 'ID'
FIRST NAME | CODE | ID
Emma | 2D1 | 2D1_Emma
Anthony | 4G3 | 4G3_Anthony
Anne Marie | 8Y2 | 8Y2_Anne
John | 5L9 | 5L9_John
I have tried it with this formula, but it is not working, it retrieves all the text in the first column instead of just the first word:
=CONCAT($B2;"_";IF($A2="* *";(LEFT($A2(FIND(" ";A2;1)-1)));A2))
If I don't use the * as a wildcard, the result I get is the same. Any other combinations I tried give an error.
Any idea how I can get it to pick the text on the left of a blank space if there is any?
Thanks!
Brisa
Concatenate a space when using FIND:
=B2&"_"&LEFT(A2,FIND(" ",A2&" ")-1)
Since your version of Excel uses ; as the separator:
=B2&"_"&LEFT(A2;FIND(" ";A2&" ")-1)
Use this formula
Assuming E2 has Code and D2 has F-Name
=E2&"_"&LEFT(D2,FIND(" ",D2)-1)

Filtering by author and counting all numbers im txt file - Linux terminal, bash

I need help with two hings
1)the file.txt has the format of a list of films
, in which they are authors in different lines, year of publication, title, e.g.
author1
year1
title1
author2
year2
title2
author3
year3
title3
author4
year4
title4
I need to show only book titles whose author is "Joanne Rowling"
2)
one.txt contains numbers and letters for example like:
dada4dawdaw54 232dawdawdaw 53 34dadasd
77dkwkdw
65 23 laka 23
I need to sum all of them and receive score - here it should 561
I tried something like that:
awk '{for(i=1;i<=NF;i++)s+=$i}END{print s}' plik2.txt
but it doesn't make sense
For the 1st question, the solution of okulkarni is great.
For the 2nd question, one solution is
sed 's/[^0-9]/ /g' one.txt | awk '{for(i=1;i<=NF;i++) sum+= $i} END { print sum}'
The sed command converts all non-numeric characters into spaces, while the awk command sums the numbers, line by line.
For the first question, you just need to use grep. Specifically, you can do grep -A 2 "Joanne Rowling" file.txt. This will show all lines with "Joanne Rowling" and the two lines immediately after.
For the second question, you can also use grep by doing grep -Eo '[0-9]+' | paste -sd+ | bc. This will put a + between every number found by grep and then add them up using bc.

Compare specific parts of two columns in a text file in Linux

I have a text file with several columns separated by tab character as below:
1 ATGCCCAGA AS:i:10 XS:i:10
2 ATGCTTGA AS:i:10 XS:i:5
3 ATGGGGGA AS:i:10 XS:i:1
4 ATCCCCGA AS:i:20 XS:i:20
I now want to compare the last two columns AS:i:(n1) and XS:i:(n2) to obtain only lines with n1 different to n2. So, my desired output would be:
2 ATGCTTGA AS:i:10 XS:i:5
3 ATGGGGGA AS:i:10 XS:i:1
Could you suggest me some ways that I can compare n1 and n2 and print out the output? Thanks in advance.
As Shawn says, you coudl do this in awk... or perl ... or sed.
An AWK example might be
awk '{split($3,a,":");split($4,b,":");if(a[3]!=b[3]) print $0}' infile.txt
If you are familiar with awk this should be fairly self explanatory

Multiple text insertion in Linux

Can someone help me how to write a piece of command that will insert some text in multiple places (given column and row) of a given file that already contains data. For example: old_data is a file that contains:
A
And I wish to get new_data that will contain:
A 1
I read something about awk and sed commands, but I don't believe to understand how to incorporate these, to get what I want.
I would like to add up, that this command I would like to use as a part of script
for b in ./*/ ; do (cd "$b" && command); done
If we imagine content of old_data as a matrix of elements {An*m} where n corresponds to number of row and m to number of column of this matrix, I wish to manipulate with matrix so that I could add new elements. A in old-data has coordinates (1,1). In new_data therefore, I wish to assign 1 to a matrix element that has coordinates (1,3).
If we compare content of old_data and new_data we see that (1,2) element corresponds to space (it is empty).
It's not at all clear to me what you are asking for, but I suspect you are saying that you would like a way to insert some given text in to a particular row and column. Perhaps:
$ cat input
A
B
C
D
$ row=2 column=2 text="This is some new data"
$ awk 'NR==row {$column = new_data " " $column}1' row=$row column=$column new_data="$text" input
A
B This is some new data
C
D
This bash & unix tools code works:
# make the input files.
echo {A..D} | tr ' ' '\n' > abc ; echo {1..4} | tr ' ' '\n' > 123
# print as per previous OP spec
head -1q abc 123 ; paste abc 123 123 | tail -n +2
Output:
A
1
B 2 2
C 3 3
D 4 4
Version #3, (using commas as more visible separators), as per newest OP spec:
# for the `sed` code change the `2` to whatever column needs deleting.
paste -d, abc 123 123 | sed 's/[^,]*//2'
Output:
A,,1
B,,2
C,,3
D,,4
The same, with tab delimiters (less visually obvious):
paste abc 123 123 | sed 's/[^\t]*//2'
A 1
B 2
C 3
D 4

Create table from multiple columns in linux, but treat fields 2, 3 (and possibly 4) as one column

Let's say I have the following file:
al.pacino Al Pacino
jerry.seinfeld Jerry Seinfeld
chad.murray Chad Michael Murray
I want to create a nice table with only two columns and treat the first name/middle name/last name as one column, like this:
al.pacino Al Pacino
jerry.seinfeld Jerry Seinfeld
chad.murray Chad Michael Murray
The problem is that if I use the "column -t" command, each field will be treated as an individual column, which is not what I want:
al.pacino Al Pacino
jerry.seinfeld Jerry Seinfeld
chad.murray Chad Michael Murray
Insert a tab between the two columns with sed - feed this oneliner the input on stdin - the output will be two tab-delimited columns
sed -r 's/ +/\t/'
I managed to do it with AWK, by reading the content from two variables (first one with login names and second with full names):
awk 'NR==FNR { a[FNR] = $0 ; if (length > max) max = length ; next } { printf "%-*s %s\n", max, a[FNR], $0 }' <(echo "${login_names}") <(echo "${full_names}")
The result is a nice looking, clean table no matter how different in length the names are:
christopher.reeve Christopher Reeve
al.pacino Al Pacino
jerry.seinfeld Jerry Seinfeld
benedict.cumberbatch Benedict Cumberbatch
chad.murray Chad Michael Murray

Resources