adding && \ to each line on a text file except last line

倖福魔咒の 提交于 2021-02-05 09:07:52

问题


I'm trying to add && \ on end of each line on a text file except the last line.

Sample input:

ps 
mkdir repo 
cd repo/ 
touch file1.txt 

Expected output:

ps && \
mkdir repo && \
cd repo/ && \
touch file1.txt 

First attempt

I tried this, but it outputs && \ on each line including the final line:

awk '{print $0"&& \\"}' RS="\r*\n\r*"

Second attempt

I tried using sed:

sed '1s/^//;$!s/$/"&&" \\/;$s/$//'

This seems to add extra newlines:

 ps
 && \
mkdir repo
 && \
cd repo/
 && \
touch file1.txt

回答1:


You could use sed for something that simple:

printf "line 1\nLine 2\nLine 3\n" | sed '$ ! s/$/ \&\& \\ /'

Output

line 1 && \ 
Line 2 && \ 
Line 3


来源:https://stackoverflow.com/questions/60319865/adding-to-each-line-on-a-text-file-except-last-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!