将列表中的字符串替换为另一个列表中的下一行

时间:2018-08-21 18:47:31

标签: linux awk sed grep

我有3个文件。一种是食物和类别的列表:

                 food="rice"
                 product="cereal"
                 food="beans"
                 product="bean"
                 food="cake"
                 product="bakery"
                 food="lettuce"
                 product="leaves"

第二个是仅列出食物的列表:

                 food="rice"
                 food="beans"
                 food="cake"
                 food="lettuce"

在第三个文件中,我的行包含/ food文件的字符串(例如/ food =“ rice”),我需要用第一个文件中列出的相应产品替换这些字符串。为了简化:在文件3的文件2中找到字符串,并用文件3的文件一的下一行替换。 我以为可能是grep和sed的组合,但我不知道该怎么做...  第三个文件看起来像这样

>[food="rice"] [some other sutff] [calories=398]
Here is a recipe with rice
>[food="beans"] [some other sutff] [calories=250]
Here is a recipe with beans
>[food="cake"] [some other sutff] [calories=100]
Here is a recipe for cake
>[food="lettuce"] [some other sutff] [calories=02]
Why would you need a recipe for lettuce?

我需要它看起来像...

 >[product="cereal"] [some other sutff] [calories=398]
 Here is a recipe with rice
 >[product="bean"] [some other sutff] [calories=250]
 Here is a recipe with beans
 >[product="bakery" [some other sutff] [calories=100]
 Here is a recipe for cake
 >[product="leaves"] [some other sutff] [calories=02]
 Why would you need a recipe for lettuce?

3 个答案:

答案 0 :(得分:1)

这是使用sed的解决方案:

sed -f <(sed 'N;s/\n/\//;s/^/s\//;s/$/\//' one) three

如果第一个文件的每一行实际上都以空格开头,则变为

sed -f <(sed 'N;s/ *//g;s/\n/\//;s/^/s\//;s/$/\//' one ) three

答案 1 :(得分:0)

这是function getFolder() { return Folder.selectDialog('Please select the folder to be imported:', Folder('~')); } function importFolderAsLayers(selectedFolder) { // if a folder was selected continue with action, otherwise quit var document; var mm = 2.83464567; // Metric MM converter… // Set the script to work with artboard rulers app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM; if (selectedFolder) { document = app.documents.add( DocumentColorSpace.RGB, width = 720*mm, height = 720*mm, ); document = app.activeDocument; var firstImageLayer = true; var newLayer; var thisPlacedItem; var posX=10; var posY=30; var count=0; // create document list from files in selected folder var imageList = selectedFolder.getFiles(); for (var i = 0; i < imageList.length; i++) { if (imageList[i] instanceof File) { var fileName = imageList[i].name.toLowerCase(); if( (fileName.indexOf(".svg") == -1) ) { continue; } else { if( firstImageLayer ) { newLayer = document.layers[0]; firstImageLayer = false; } else { newLayer = document.layers.add(); } // Give the layer the name of the image file newLayer.name = fileName.substring(0, fileName.indexOf(".") ); // Place the image on the artboard newGroup = newLayer.groupItems.createFromFile( imageList[i] ); newGroup.position = [ posX , posY ]; } } posX += newGroup.width; if(posX > (newGroup.width*16)) { posX = 0; posY -= newGroup.height; } } if( firstImageLayer ) { // alert("The action has been cancelled."); // display error message if no supported documents were found in the designated folder alert("Sorry, but the designated folder does not contain any recognized image formats.\n\nPlease choose another folder."); document.close(); importFolderAsLayers(getFolder()); } } else { // alert("The action has been cancelled."); // display error message if no supported documents were found in the designated folder alert("Rerun the script and choose a folder with images."); //importFolderAsLayers(getFolder()); } } // Start the script off importFolderAsLayers( getFolder() ); 组合

sed/awk

答案 2 :(得分:0)

我看不到file2的用途。

$ cat tst.awk
BEGIN { FS="[][]" }
NR==FNR {
    gsub(/^[[:space:]]+|[[:space:]]+$/,"")
    if (NR%2) { food = $0 }
    else { map[food] = $0 }
    next
}
{
    sub(/\[[^][]+/,"["map[$2])
    print
}

$ awk -f tst.awk file1 file3
>[product="cereal"] [some other sutff] [calories=398]
Here is a recipe with rice
>[product="bean"] [some other sutff] [calories=250]
Here is a recipe with beans
>[product="bakery"] [some other sutff] [calories=100]
Here is a recipe for cake
>[product="leaves"] [some other sutff] [calories=02]
Why would you need a recipe for lettuce?