更改文件使用bash的第一线 [英] change first line of a file using bash

查看:92
本文介绍了更改文件使用bash的第一线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要改变一行在文件中。它总是在该文件的第一行。

它看起来像:

  ^ h \\ TN0 N1 N2 N3 N4 N5 N6 N7 N8 N9 hilu CJT 1百万

有在所有的空白标签除了小时后。

我需要重新改造行成

  ^ h N1 N2 N3 N4 N5 N6 N7 N8 N9
hilu CJT 1百万

开头澳线\\ t的事情,N0需要去需要有H和N1之间的标签。然后换行之前需要的 hilu 的开始,但应该有N9后,没有额外的标签

在理想情况下,我只想养活我的文件,脚本,它不会要求写一个脚本,中间填。

有可能在Perl或Python左右一个高效的版本?我想关于R但随后有文件中的行,只有第一留置权需要1000来改变...

试图利用从 jahid 解决从研发运行它以

 >系统(膏(SED -r \\'。1S /(*)\\吨(地区*)/ \\ 1 \\ n \\ 2 /; 1S / \\\\ T [^ [:空间:]] + // \\' ,arg_t1))
SED:-e前pression#1,烧焦20:未结束的'S'命令

与从COMM建议我得到

 > 。系统(膏(SED -r \\1 /(*)\\吨(地区*)/ \\ 1 \\ n \\ 2 /; 1S / \\\\ T [^ [:空间:]] + // \\ ,arg_t1))
SED:-e前pression#1,烧焦20:未结束的'S'命令


解决方案

使用 SED (带扩展正则表达式):

(。*)

  SED -r'1S / \\吨(。hilu *)/ \\ 1 \\ n \\ 2 /; 1S / \\\\ T [^ [:空间:] ] + //'文件

要更改文件就地:

(。*)

  SED -r --in就地'1S / \\吨(。hilu *)/ \\ 1 \\ n \\ 2 /; 1S / \\\\ T [^ [:空间:]] + //'文件

I need to change a single line in a file. It is always in the first line of the file.

It looks like:

h\tn0   n1  n2  n3  n4  n5  n6  n7  n8  n9  hilu    cjt 1   1000000

there is a tab in all gaps except after the h.

I would need to re-transform the line into

h  n1   n2  n3  n4  n5  n6  n7  n8  n9  
hilu    cjt 1   1000000

at the beginning o the line the \t thing and n0 needs to go and there needs to be a tab between h and n1. Then a newline needs to start before hilu but there should be no additional tab after n9

Ideally I would just feed my file to the script and it would not require writing an intermediate script to fill.

is there maybe an efficient version in Perl or python or so? I thought about R but then there are 1000 of lines in the file and only the first lien needs be changed...

tried to use the solution from jahid to run it from r with

> system(paste("sed -r \'1s/(.*)\t(REGION.*)/\1\n\2/;1s/\\t[^[:space:]]+//\'","arg_t1")) 
sed: -e expression #1, char 20: unterminated `s' command

with the suggest from the comm I get

> system(paste("sed -r \"1s/(.*)\t(REGION.*)/\1\n\2/;1s/\\t[^[:space:]]+//\"","arg_t1")) 
sed: -e expression #1, char 20: unterminated `s' command

解决方案

Using sed (with Extended Regex):

sed -r '1s/(.*)\t(hilu.*)/\1\n\2/;1s/\\t[^[:space:]]+//' file

To change the file inplace:

sed -r --in-place '1s/(.*)\t(hilu.*)/\1\n\2/;1s/\\t[^[:space:]]+//' file

这篇关于更改文件使用bash的第一线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆