sort in bash, sort based part of column - linux

I have a file like:
a1 blah
b2 blah
a3 blah
b1 blah
b3 blah
a2 blah
if I do
sort -k1,1 file.name
I'll get this:
a1
a2
a3
b1
b2
b3
However, I want to get this order:
a1
b1
a2
b2
a3
b3
how can I do that? Thanks
Edit: I edited the example, the previous one didn't present the whole problem

You are looking for sort -kN.M! N.M indicates sort to start from the Mth character on Nth field.
Initial solution:
sort -k1.2 your_file
Updated one:
sort -k1.2,k1.2 your_file
so it will just sort by this specific character and won't go further.
Output:
a1 blah
b1 blah
a2 blah
b2 blah
a3 blah
b3 blah

Related

sed : Insert line after n lines following the pattern

Pls bear with me as I knew this questions has been asked a few time by others, yet I keep getting error with the suggested answers.
Original file:
a1
a2
a3
product 2
b1
b2
b3
product 3
c1
c2
c3
I would like to add string '1111111' two lines after match pattern 'product', fetch to a file 'out'. Such like:
product 1
a1
a2
1111111
a3
product 2
b1
b2
1111111
b3
product 3
c1
c2
1111111
c3
Those links I referred are suggesting the command as below but I get an error:
sed '/product/{n;n;a \ 1111111'} file > out
sed: -e expression #1, char 0: unmatched `{'
I would like to achieve this using sed?
These are links I'm refering:
Insert line after match using sed
sed - insert line after X lines after match
Thank you.
Either adding the -e option as Hatless suggested, or add one linebreak after your a command:
$ sed '/product/{n;n;a\ 1111111
}' f
product 1
a1
a2
1111111
a3
product 2
b1
b2
1111111
b3
product 3
c1
c2
1111111
c3

Increment row reference based on index in selection, and not same row number between sheets

I have a workbook containing two sheets. Sheet 1 has values in column A for every row up to row number 2000. Sheet 2 should duplicate the values over multiple rows for each row in Sheet 1. Like this:
Sheet1:
a1 | 123
a2 | 456
a3 | 789
and for Sheet2:
Sheet2:
a1 | 123
a2 | 123
a3 | 456
a4 | 456
a5 | 789
a6 | 789
The duplication is fairly simple, where I just put a reference of the next rows to the row collecting the row value from Sheet1:
a2: =a1
However, selecting and dragging rows a1 and a2 in Sheet2 to get the corresponding formulas copied over to the next rows, the formula does not reference the correct rows in Sheet1. Something like this occurs:
Sheet2:
a1 | 123
a2 | 123
a3 | 789
a4 | 789
Where cell a3 in Sheet2 references cell a3 in Sheet1, instead of cell a2 which is the next row. I have tried several functions with index, offset etc. but none of them seem to circumvent the automatic same-row-reference between the worksheets. Any quick ideas?
"generic approach" imho is either one of these.. :
Just edit the "2" in the 1st comment ans.
use ROW() and argument for OFFSET()
'Manually' build the reference using INDIRECT
set the 1st 2 row manually, 3rd row onward use =IF(A2=A1,INDIRECT("Sheet1!A"&(row()+1)/2,TRUE),A2) and drag downwards.
Is this what you are looking for?

Excel formula to separate text between "\"

I have a path in
A1 C:\Users\fe\Desktop\01 Tur\2015\Kauk\Telu\Frame Report.pdf
A2 C:\Users\fe\Desktop\01 Tur\Deliveries\10 Toim\Alh\2005\Moot\CMC.doc
A3 C:\Users\fe\Desktop\01 Tur\Equip\Set\M-R\Kir\G3\sen.xls
etc.
I would like to separate these paths to (example for A1)
A2 "Users" | A3 "fe" | A4 "Desktop" | A5 "01 Tur" | A6 "2015" | A7 "Kauk" | A8 "Telu" | A9 "Frame Report.pdf"
I have tried to play with
=IF(ISERROR(FIND("\";A1;FIND("\";A1;1)+2));A1;LEFT(A1;FIND("\";A1;FIND("\";A1;1)+2)))
but it is not so suitable for multiplication. Is there any better solution that can be copied for this case?
With data in A1, in B1 enter:
=TRIM(MID(SUBSTITUTE($A1,"\",REPT(" ",999)),COLUMNS($A:A)*999-998,999))
and copy across:

Excel Find formula split string of names

I'm trying to split a list of names, They in the form of:
Surname, Title. Firstname (Nickname)
Jonesy, Mr. Cheese (Edam)
Bukowski, Mrs. Gertrude (Gerti)
I need to put them into Firstname Surname
It should be easy to pick up the Firstname: Two characters after the dot of Title up to the space before the left bracket of Nickname. Only my excel formula doesn't give me what I want.
A1 Smith, Mr. Andrew (Andy)
A2 =MID(A1,1,Find(" ",A1)-2)
A3 =MID(A1,Find(".",A1)+2, Find("(",A1)+1)
Only for A3 I get:
Andrew (Andy)
Instead of just the name Andrew
I'll add the caveat that I need the formula to refer to the name cell and not an cell with temp data to be referenced (ie an interim cell)
I can't see the woods for the trees on this one. Need to call in a bigger brain.
Assuming a data setup where headers are in row 1, with your "Complete Name" in column A starting in row 2, like so:
In cell B2 and copied down is this formula to get the first name:
=MID(A2,FIND(".",A2)+2,FIND("(",A2)-FIND(".",A2)-3)
In cell C2 and copied down is this formula to get the last name:
=LEFT(A2,FIND(",",A2)-1)
You can use this single formula to get the Firstname Surname:
=TRIM(MID(A1,SEARCH(".",A1)+1,SEARCH("(",A1)-SEARCH(".",A1)-1))&" "&MID(A1,1,FIND(" ",A1)-2)
Pretty Simple I think....(Famous last words).
You had:
A1 = Smith, Mr. Andrew (Andy)
A2 =MID(A1,1,Find(" ",A1)-2)
A3 =MID(A1,Find(".",A1)+2, Find("(",A1)+1)
I replaced the A# Find "(" with " " and it worked.
A1 = Smith, Mr. Andrew (Andy)
A2 =MID(A1,1,Find(" ",A1)-2)
A3 =MID(A1,Find(".",A1)+2, Find(" ",A1)+1)
This will only work if you have a single Firstname though. A name like Bobby Joe or Billy Bob will cut off at Bobby and Billy.

I would like to prepend an ascending set of numbers to a row of text cells in Excel

So I have a row of cells that have questions
Blah | blah2 | blah3 ||| blah73
I'd like to make it so it looks like this
Q1: Blah | Q2: Blah2 |||| Q73: blah73
How do I do this in excel without having to do it manually?
In an empty row:
="Q" & Column(A:A) & " " & A1
Where A1 is the first cell in the row with Blah. You only need to change the A1 to the first cell and drag across

Resources