sed : Insert line after n lines following the pattern - linux

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

Related

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?

pyspark - Read files with custom delimiter to RDD?

I am newbie in pyspark, and I'm trying to read and merge RDD rows into one row.
Assuming that I have the following text file:
A1 B1 C1
A2 B2 C2 D3
A3 X1 YY1
DELIMITER_ROW
Z1 B1 C1 Z4
X2 V2 XC2 D3
DELIMITER_ROW
T1 R1
M2 MB2 NC2
S3 BB1
AQ3 Q1 P1"
Now, I want to combine all rows appears in each section (between DELIMITER_ROW) into one row, and return a list of these merged rows.
I want to create this kind of list:
[[A1 B1 C1 A2 B2 C2 D3 A3 X1 YY1]
[Z1 B1 C1 Z4 X2 V2 XC2 D3]
[T1 R1 M2 MB2 NC2 S3 BB1 AQ3 Q1 P1]]
How can It be done in pyspark using RDD?
For now I know how to read the file and filter out the delimiter rows:
sc.textFile(pathToFile).filter(lambda line: DELIMITER_ROW not in line).collect()
but I don't know how to reduce/merge/combine/group the rows in each section into one row.
Thanks.
Rather than reading and splitting, You can use hadoopConfiguration.set to set the delimiter which separates the row and then split the row.
spark.sparkContext.hadoopConfiguration.set("textinputformat.record.delimiter", "DELIMITER_ROW")
Hope this helps!

Sum fields in a column if there is an entry in a corresponding row in another column

Assume the following data:
| A B C
--+------------------------
1 | 2 3 5
2 | 2 3
3 | 4 4
4 | 2 3
5 | 5 6
In cell A6, I want Excel to add cells C1, C2, C3 on the basis that A1, A2 and A3 have data in. Similarly, I want B6 to add together C1, C4 and C5 because B1, B4 and B5 have data.
Can someone help?
In A6 enter:
=SUMPRODUCT(($C1:$C5)*(A1:A5<>""))
and then copy to B6:
A simple SUMIF formula will work
=SUMIF(A$1:A$5,"<>",$C$1:$C$5)
Place that formula is cell A6 and then copy it to B6.
You can create another column, e.g. AValue, with the formula =IF(ISBLANK(A1),0,A1) in it. This will return 0 if the cell in A in the corresponding line is empty, or the value from the cell in A otherwise.
Then you can just sum up the values of the new column.

Find text in column one and copy value from next column to column 3 in Excel [duplicate]

This question already has answers here:
Check if Cell value exists in Column, and then get the value of the NEXT Cell
(3 answers)
Closed 8 years ago.
Column 1 in my spread sheet contains text (just 1 word), 2nd column numbers and column 3 will have those numbers in specific order.
Example:
cell values
A3 aaaa
A4 bbbb
A5 cccc
A6 dddd
A7 eeee
A8 ffff
B3 11
B4 22
B5 33
B6 44
B7 55
B8 66
I want cell C3 to search the whole column A for a word ffff and once it found it, it would copy a value of the cell next to it in column B. So if the word ffff was in cell A8 the cell C3 would be 66 as B8=66. If ffff was in A5 than C3 would be 33 etc. After that I want C4 to find a word cccc in column A and copy value from next cell in column B and so on. Does anyone know how can I do it? Can this be done without using macros?
You can use VLOOKUP function, e.g. in C3
=VLOOKUP("ffff",A:B,2,FALSE)
the 2 tells Excel to return the value from column 2 of the specified range (A:B) and FALSE means that only exact matches are considered - see Excel help for more on VLOOKUP

sort in bash, sort based part of column

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

Resources