从文件中读取一行,将行替换为另一个文件

时间:2019-01-08 15:34:52

标签: bash shell

我是Linux脚本新手。 我需要一个文件中的行,然后将其逐行写入另一个文件的特定行中。

示例:

File1.txt:

line1
line2
line3

File2.txt:

abc
abc
xxx

我需要先写“ line1”而不是File2.txt的第三行,然后对该文件进行一些操作,然后写“ line2”而不是File2.txt的第三行,依此类推。

目前这就是我所拥有的

    for n in {1..5} 
do
    a=$(sed '24!d' File1) #read string 24
    echo $a
    sed -i '1s/.*/a/' File2.txt

done

现在我应该在循环中使用变量n而不是第3行中的24。可能吗? 第5行中是同一件事,其中>“ a”应该是变量,但是程序将File2.txt的第一行更改为“ a”。 我可以使用此功能还是需要使用其他功能(如果可以,是什么功能?)?

1 个答案:

答案 0 :(得分:0)

尝试

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:textSize="23sp"
        android:text="The Beach Beneath the Street"
        android:fontFamily="sans-serif-medium"
        android:textColor="@color/black"
        android:textIsSelectable="true"/>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:padding="15dp"
        android:fontFamily="sans-serif-condensed"
        android:lineHeight="25dp"
        android:lineSpacingMultiplier="1"
        android:lineSpacingExtra="6dp"
        android:textSize="16sp"
        android:text="Never add a RecyclerView or ListView to a scroll view"
        android:textIsSelectable="true" />

</RelativeLayout>

如果要对文件2执行任何操作,只需使用

sed '2r t2' t1

我认为这可以解决您的问题。

sed 2r<(cat t2) t1  ##You can change command (cat t2) as per your need

详细信息

2:第二行

r:从文件中读取

如果要提交此操作,可以使用sed -i选项。有关更多详细信息,请参见man sed

感谢Benjamin W.丢失的情况(sed'2r t2't1)

相关问题