如何删除文件中的前N行,并使用bash保留其余行

时间:2018-03-16 19:36:49

标签: linux bash shell scripting

我尝试使用sed删除文件中的第一个块,并保留剩余的最后一行并将其保存到文件中。但是,文件的最后几行未保存。

我使用此代码删除前15行:

sed '1,15d' local_accounts.txt > remaining_accounts.txt

然而,最后一行也被删除了。

local_accounts.txt

dn: uid=Nemuadmin,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: Nemuadmin
uid: Nemuadmin
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 10001
gidNumber: 557
homeDirectory: /home/Nemuadmin
loginShell: /bin/bash
shadowMin: 0
shadowMax: 99999
shadowWarning: 7

dn: uid=toosa,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: toosa
uid: toosa
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 10000
gidNumber: 400
homeDirectory: /home/toosa
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

dn: uid=test_user1,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: test_user1
uid: test_user1
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 10200
gidNumber: 10200
homeDirectory: /home/test_user1
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

dn: uid=test_user2,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: test_user2
uid: test_user2
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 20000
gidNumber: 20000
homeDirectory: /home/test_user2
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

dn: uid=test_user3,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: test_user3
uid: test_user3
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 30000
gidNumber: 30000
homeDirectory: /home/test_user3
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

dn: cn=test_group1,ou=Group,fsFragmentId=Security,fsClusterId=ClusterRoot
objectClass: FSSecGroup
objectClass: posixGroup
objectClass: groupOfNames
objectClass: FSSecAccessControl
objectClass: extensibleObject
cn: test_group1
gidNumber: 10200
member: uid=test_user1,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
memberUid: test_user1

dn: cn=test_group2,ou=Group,fsFragmentId=Security,fsClusterId=ClusterRoot
objectClass: FSSecGroup
objectClass: posixGroup
objectClass: groupOfNames
objectClass: FSSecAccessControl
objectClass: extensibleObject
cn: test_group2
gidNumber: 20000
member: uid=test_user2,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
memberUid: test_user2

dn: cn=test_group3,ou=Group,fsFragmentId=Security,fsClusterId=ClusterRoot
objectClass: FSSecGroup
objectClass: posixGroup
objectClass: groupOfNames
objectClass: FSSecAccessControl
objectClass: extensibleObject
cn: test_group3
gidNumber: 30000
member: uid=test_user3,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
memberUid: test_user3
dn: fssecPermissionId=PAMCLI,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
fssecAssignedGroupRoleRef: _oms_sudoer_ptally
fssecPermissionCLICommand: /sbin/pam_tally2
objectClass: FSSecPermission
objectClass: FSSecCLIPermission

dn: fssecPermissionId=perm1,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
objectClass: FSSecPermission

dn: fssecPermissionId=perm2,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
objectClass: FSSecPermission

dn: fssecPermissionId=perm3,fsFragmentId=Permissions,fsFragmentId=Security,fsClusterId=ClusterRoot
objectClass: FSSecPermission

执行sed命令后:

remaining_accounts.txt

dn: uid=toosa,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: toosa
uid: toosa
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 10000
gidNumber: 400
homeDirectory: /home/toosa
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

dn: uid=test_user1,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: test_user1
uid: test_user1
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 10200
gidNumber: 10200
homeDirectory: /home/test_user1
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

dn: uid=test_user2,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: test_user2
uid: test_user2
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 20000
gidNumber: 20000
homeDirectory: /home/test_user2
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

dn: uid=test_user3,ou=People,fsFragmentId=Security,fsClusterId=ClusterRoot
cn: test_user3
uid: test_user3
objectClass: FSSecAccount
objectClass: posixAccount
objectClass: shadowAccount
objectClass: extensibleObject
uidNumber: 30000
gidNumber: 30000
homeDirectory: /home/test_user3
loginShell: /bin/bash
shadowMax: 180
shadowWarning: 30
shadowInactive: 0

如何删除第一个块并保留其他块?提前谢谢。

1 个答案:

答案 0 :(得分:1)

轻量级替代方案可能是使用tail,您可以执行以下操作:

tail -n +15 local_accounts.txt > remaining_accounts.txt

-n +15表示从第15行开始。

这将保留未触及的文件local_accounts.txt,并使用remaining_accounts.txt的相同内容创建local_accounts.txt,但仅限于第15行。