如何从linux中的文件中获取两列?

时间:2013-12-10 06:04:55

标签: linux shell awk amazon-ec2

 for snapshot in $(cat "$EC2_HOME/SnapshotsDOW_$today_date" | awk '{print $2}')
 do
    ec2-delete-snapshot $snapshot
 done

此代码现在提取一列我必须获取两个列,我该怎么做并将值存储在变量中:

:: Create snapshots of all attached volumes
for /F "tokens=2,3" %%d IN (' type "%EC2_HOME%\Working\ActiveVolumes_%date-today%.txt" ') do for /F "tokens=3,5*" %%a IN (' type "%EC2_HOME%\Working\InstanceNames_%date-today%.txt" ') do if %%a EQU %%e call ec2-create-snapshot "%%d" -d "%date-today%: Daily Backup for %%b (VolID:%%d InstID:%%e)"

这是我要转换为shell脚本的批处理代码

我试过这个:

#Create snapshots of all attached volumes
for volumeid,instanceid in $(cat "$EC2_HOME/ActiveVolumes_$today_date" | awk '{print $2,$3}') 
  do 
   for instance_id,name in $(cat "$EC2_HOME/Instances_$today_date" | awk"{print $3,$5}')
     do
     if ["$instanceid" -eq "$instance_id" ]
     then
        do ec2-create-snapshot "$volumeid" -d "$today_date: Daily Backup for $instance_id (VolID:$volumeid InstID:$instance_id)"
        done
        fi
    done
  done

其中volume.txt的内容为

ATTACHMENT vol-fa0 i-26011 /dev/sda1 attached 2013-11-20T06:42:49+0000 true ATTACHMENT vol-36b i-e3d6 /dev/sda1 attached 2013-11-21T12:38:09+0000 true

和instances.txt是

TAG instance i-42370 Name Linux_Test

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你正在寻找:

awk '{print $2, $3}' "$EC2_HOME/ActiveVolumes_$today_date" | while read vol_id inst_id; do
    awk '{print $3, $5}' "$EC2_HOME/Instances_$today_date" | while read inst_id2 name; do
        if test "$inst_id" = "$inst_id2"; then
            ec2-create-snapshot "$vol_id" -d "$today_date: Daily Backup for $inst_id (VolID:$vol_id InstID:$inst_id)"
        fi
    done
done