Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

311
Visualizações
Get first character of previous line

Replace the first character of the current line with the first character of the previous line. For example Input file:

Mark was
Great friend
And

Output file:

Mark was
Mreat friend
Gnd

Must be done with sed

I do not understand how to make the letter replace exactly the beginning of the word on the previous line, all I could do was add a character to the end of the line, but not to the beginning:

sed '2,$ s/[^.]/& &/; 2,$ s/ /\n/' file.txt | paste -d ' ' - -

Output:

Mark was G
Great friend A
And

I don't understand how exactly to specify in the regular expression to work at the beginning of the previous line.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

sed is certainly the wrong tool for this! But this seems to do what you want:

sed -e 1's/./&&/' -e H -e 's/\(.\).*/\1/' -e x -e 's/\n.//' input

On the first line, you duplicate the first character to prime the hold space. For all lines, you append the line to the hold space (for this first line, this is the modified line with the first character duplicated). Then you remove all but the first char and swap the pattern space with the hold space. Then you remove the newline and the first character of the line, leaving the first character of the previous line and the remainder of the line in the pattern space, while only the first character is in the hold space. Print the pattern space and repeat.

over 4 years ago · Santiago Trujillo Relatório

0

With your shown samples please try following awk code. Simple explanation would be, if line number is more than 1 then print prev(which has first character of previous line) as first character and then print rest of line. And set each line's first character to prev variable to become first for its next line.

awk '
FNR==1{ print }
FNR>1 {
  print prev substr($0,2)
}
{
  prev=substr($0,1,1)
}
' Input_file

OR more precisely:

awk '
FNR>1 {
  $0 = prev substr($0,2)
}
{
  prev=substr($0,1,1)
}
1
' Input_file
over 4 years ago · Santiago Trujillo Relatório

0

This might work for you (GNU sed):

sed -E '1h;x;G;s/(.).*\n./\1/' file

Save the first line.

Swap to the copied line, append the current line to it, remove everything between the first character in the copied to the second character in the current line and print it.

N.B. The current line, now becomes a copy and the process repeats to end of file.


If each line always begins with a word of length two or more:

 sed -E '1h;x;G;s/\B.*\n.//' file
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda