简体   繁体   English

使用awk或bash中的sed编辑文本

[英]Editing text using awk or sed in bash

The content of my file.txt is something like this: 我的file.txt的内容是这样的:

fghfg
sdfsd
thrt
wefs
dfd
htyj
dfd
gsedf
     1     sdfsdf
     3     sdfghj
     f     fgdfhtyu
dfdsfsd
sdgdfg
sdfzcxzc
fgjty

I want to change this part: 我要更改此部分:

1     sdfsdf
3     sdfghj
f     fgdfhtyu

to this one: (by removing first column and spaces) 为此:(删除第一列和空格)

sdfsdf
sdfghj
fgdfhtyu

Then redirect the whole output of file.txt to file2.txt . 然后将file.txt的整个输出重定向到file2.txt

How is it possible...? 这怎么可能...?

Thanks 谢谢

Try changing your script to this: 尝试将脚本更改为此:

find "$1" -print |
while IFS= read -r line; do
    awk '!a[$0]++{print $NF}' "$line" > /tmp/file.txt
done

Does anything need to change? 有什么需要改变的吗?

Using sed , you can do something like this 使用sed ,您可以执行以下操作

sed 's/^ \+[^ ] \+//' file1.txt > file2.txt

It will remove all sequences at the begining of the line that contain a sequence of whitespaces, followed by a single non whitespace character than a sequence of whitespaces. 它将删除行开头的所有序列,这些序列包含一系列空格,后跟一个非空格字符,而不是一系列空格。 Using the file in your question, the results is: 使用问题中的文件,结果为:

fghfg
sdfsd
thrt
wefs
dfd
htyj
dfd
gsedf
sdfsdf
sdfghj
fgdfhtyu
dfdsfsd
sdgdfg
sdfzcxzc
fgjty

This should do: 应该这样做:

awk '{print $NF}' file.txt > file2.txt

or shortest solution of the day: 或一天中最短的解决方案:

awk '{$0=$NF}1' file.txt > file2.txt
cat file2.txt
fghfg
sdfsd
thrt
wefs
dfd
htyj
dfd
gsedf
sdfsdf
sdfghj
fgdfhtyu
dfdsfsd
sdgdfg
sdfzcxzc
fgjty

Just print the last column. 只需打印最后一列。

另一个简单的sed命令,

sed 's/.* \(.*\)/\1/g' file1.txt > file2.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM