将日期从文本文件的第一行追加到其他行的开头

时间:2019-04-07 14:18:58

标签: bash awk sed append

我正在尝试将文本的第一行(日期)附加到文本文件其余部分中每一行的开头。有人可以帮忙吗?我正在使用bash将数据提取到显示的文本文件中。 (对此完全陌生)

Text File as discussed above

当前文本文件:

Sun Apr 7 14:16:30 
Host: 192.168.1.3; Status: Up
Host: 192.168.1.5; Status: Up
Host: 192.168.1.6; Status: Up

我希望文本文件读取:

Date (from first line); Host: 192.168.1.3; Status: Up
Date (from first line); Host: 192.168.1.5; Status: Up
Date (from first line); Host: 192.168.1.6; Status: Up

4 个答案:

答案 0 :(得分:4)

awk -v OFS='; ' 'NR==1{d=$0;next}{print d,$0}' file

答案 1 :(得分:2)

awk下面可以帮助您:

awk 'NR==1{Date="Date" FS $0 ";" FS}NR!=1{print Date $0}' file

示例输出

Date Sun Apr 7 14:16:30; Host: 192.168.1.3; Status: Up
Date Sun Apr 7 14:16:30; Host: 192.168.1.5; Status: Up
Date Sun Apr 7 14:16:30; Host: 192.168.1.6; Status: Up

答案 2 :(得分:1)

这可能对您有用(GNU sed):

import arcpy
# iterating all tables in an environment , and make join them with a shapefile

# these are constant variables
shapefilepath = r"c:\users\someplace\someshape.shp"
commoncolumn = "SAMECOLUMN"  # this column must be same in other shapefiles too
# If all shapefile samecolumns are different each other, you need to make a list like this
commoncolumns_ordered = ['SAMECOLUMN1', 'SAMECOLUMN2', ]  # .. goes away
mainfolder = r"c:\users\someplace"
tablegdb = r"c:\users\someplace\somegdb.gdb"  # we'll search our tables here

arcpy.env.workspace = tablegdb  # we will work on here
mytables = arcpy.ListTables("*")  # you can filter your tables as starting or ending with a letter.

for table in mytables:
    # you need to make view from all tables
    name = arcpy.Describe(table).name.encode('utf-8')  # my table name
    table_view = arcpy.MakeTableView_management(table, name)

    # ok so we have our view. Otherwise, we would not be able to use this as an input for add join tool
    """
    There are couple differences between add join and join field tools. Read them:
    Add join help : https://pro.arcgis.com/en/pro-app/tool-reference/data-management/add-join.htm
    Join field help : https://pro.arcgis.com/en/pro-app/tool-reference/data-management/join-field.htm

    * We don't have to make table view if we use join field
    """

    # i assume that both common columns, # fields are same.
    out_join = arcpy.AddJoin_management(table_view, commoncolumn, shapefilepath, commoncolumn)

    # extracting them is not useful. But I'll write it down:
    arcpy.Copy_management(out_join, out_data="%s\\%s" % (out_gdb, name))

# some notes:
# If your samecolumn fields are different between each other in tables
# you need to iterate them like this:
for table, column in zip(mytables, commoncolumns_ordered):
    print (table)
    print (column)
    # do others

复制第一行。

删除第一行。

将第一行追加到当前行。

将第一行重新排列到当前行的前面,并在其前跟文字sed '1h;1d;G;s/\(.*\)\n\(.*\)/Date \2; \1/' file ,然后是Date

答案 3 :(得分:0)

在gnu awk上尝试过,相对于第一行#

 awk 'NR==1{a=$0;next}{print "Date "a,$0}' d

对图案的尊重

awk '$0~/\s*(Sun|Mon|Tue|Wed|Thu|Fri|Sat)/{a=$0;next}{print "Date "a,$0}' d