awk打印格式化的所有列

时间:2015-06-12 10:37:25

标签: awk pattern-matching

我想在未格式化的输入下面格式化。如果第二列以“2”开头,我想打印所有字段。

实际文件包含许多列。是否有最简单的方法来打印所有列而不是键入print $2,$3,$4,$5,...$n

Input.csv

---------------------------
|Data statistics|Number of|
|-------------------------|
|Records passed |  750,517|
---------------------------
Transaction Data for Serial Numbers

No of Records :                                                      750517

-----------------------------------------------------------------------------------------------------------
|Material Number|Serial Number From |Serial Number To   |Document Year|Delivery Document|Material Document|
|----------------------------------------------------------------------------------------------------------
|200965         |18604165309338     |18604165309387     |2012         |6100202749       |AAA2778202       |
|201163         |10740000822407     |10740000822606     |2012         |6100202749       |AAA2778202       |
|201232         |18604177741067     |18604177741366     |2012         |6100202749       |AAA2778202       |
|201295         |18604221522337     |18604221523836     |2012         |6100202749       |AAA2778202       |
|201480         |18604113309952     |18604113310131     |2012         |6100202749       |AAA2778202       |
|201781         |18604199150436     |18604199150835     |2012         |6100202749       |AAA2778202       |
|201480         |6001400030046472   |6001400030046771   |2012         |6100202520       |AAA2777953       |
|202853         |6001700000180323   |6001700000180722   |2012         |6100202520       |AAA2777953       |
-----------------------------------------------------------------------------------------------------------

我在命令下面没有注意到:

awk ' BEGIN {FS ="|"; OFS = ","} { if ($2~"2") print $2,$3,$4,$5,$6,$7 }' Input.txt

期望的输出:

200965         ,18604165309338     ,18604165309387     ,2012         ,6100202749       ,AAA2778202       
201163         ,10740000822407     ,10740000822606     ,2012         ,6100202749       ,AAA2778202       
201232         ,18604177741067     ,18604177741366     ,2012         ,6100202749       ,AAA2778202       
201295         ,18604221522337     ,18604221523836     ,2012         ,6100202749       ,AAA2778202       
201480         ,18604113309952     ,18604113310131     ,2012         ,6100202749       ,AAA2778202       
201781         ,18604199150436     ,18604199150835     ,2012         ,6100202749       ,AAA2778202       
201480         ,6001400030046472   ,6001400030046771   ,2012         ,6100202520       ,AAA2777953       
202853         ,6001700000180323   ,6001700000180722   ,2012         ,6100202520       ,AAA2777953 

3 个答案:

答案 0 :(得分:4)

set LOOPS to 3
set soundFolderName to "sounds_dir"

# ---------------------
# SETUP 
# ---------------------
set soundFolderFullPath to my composeFilePath(soundFolderName)

tell application "Finder"

    if folder soundFolderFullPath exists then

        set soundFolder to (folder soundFolderFullPath)

    else
        set soundFolder to ""
        # Customize action when folder does not exist
        beep
        log "*** Error: folder " & quoted form of soundFolderFullPath & " missing!"
        return
    end if

    if (count files in soundFolder) is 0 then
        # Customize action when folder has no items in it
        beep
        log "*** Error: No items in " & quoted form of soundFolderFullPath & " !"
        return
    end if

end tell

# -------------------------------------------
# We have "soundFolder" and it has items in it
# -------------------------------------------

repeat LOOPS times

    # PLAY RANDOM FILE

    # As ONE LINER:
    ## do shell script "/usr/bin/afplay " & quoted form of (POSIX path of ((some file of soundFolder) as text)) & " > /dev/null 2>&1 &"

    # Step By Step
    set aRandomFile to some file of soundFolder
    set aRandomFile to POSIX path of (aRandomFile as text)

    set shellScript to "/usr/bin/afplay " & quoted form of aRandomFile & " > /dev/null 2>&1 &"
    do shell script shellScript

    delay 1

end repeat




on composeFilePath(fileName)
    if fileName is "" then return ""
    set pathToMe to path to me -- this is the full path to this script
    -- get the folder this script is in:
    set thisScriptsFolder to ""
    tell application "Finder"
        try
            set thisScriptsFolder to (get container of pathToMe) as text
        end try
    end tell
    if thisScriptsFolder is "" then
        return ""
    end if
    return thisScriptsFolder & fileName -- full path
end composeFilePath

要在下面发表评论:

$ awk 'gsub(/\|/,",") && gsub(/^,|,$/,"") && /^2/' file
200965         ,18604165309338     ,18604165309387     ,2012         ,6100202749       ,AAA2778202
201163         ,10740000822407     ,10740000822606     ,2012         ,6100202749       ,AAA2778202
201232         ,18604177741067     ,18604177741366     ,2012         ,6100202749       ,AAA2778202
201295         ,18604221522337     ,18604221523836     ,2012         ,6100202749       ,AAA2778202
201480         ,18604113309952     ,18604113310131     ,2012         ,6100202749       ,AAA2778202
201781         ,18604199150436     ,18604199150835     ,2012         ,6100202749       ,AAA2778202
201480         ,6001400030046472   ,6001400030046771   ,2012         ,6100202520       ,AAA2777953
202853         ,6001700000180323   ,6001700000180722   ,2012         ,6100202520       ,AAA2777953

答案 1 :(得分:2)

你可以说:

$ awk -F"|" -v OFS="," '$2~/^2/ {NF--; $0=$0; print}' file
,200965         ,18604165309338     ,18604165309387     ,2012         ,6100202749       ,AAA2778202       
,201163         ,10740000822407     ,10740000822606     ,2012         ,6100202749       ,AAA2778202       
,201232         ,18604177741067     ,18604177741366     ,2012         ,6100202749       ,AAA2778202       
,201295         ,18604221522337     ,18604221523836     ,2012         ,6100202749       ,AAA2778202       
,201480         ,18604113309952     ,18604113310131     ,2012         ,6100202749       ,AAA2778202       
,201781         ,18604199150436     ,18604199150835     ,2012         ,6100202749       ,AAA2778202       
,201480         ,6001400030046472   ,6001400030046771   ,2012         ,6100202520       ,AAA2777953       
,202853         ,6001700000180323   ,6001700000180722   ,2012         ,6100202520       ,AAA2777953    

答案 2 :(得分:1)

这可能对您有所帮助

background-image: url(../images/top-rept.jpg);

<img src="images/img1.jpg" alt="" height="101"
 width="157">

<强>输出

$ cat file
---------------------------
|Data statistics|Number of|
|-------------------------|
|Records passed |  750,517|
---------------------------
Transaction Data for Serial Numbers

No of Records :                                                      750517

-----------------------------------------------------------------------------------------------------------
|Material Number|Serial Number From |Serial Number To   |Document Year|Delivery Document|Material Document|
|----------------------------------------------------------------------------------------------------------
|200965         |18604165309338     |18604165309387     |2012         |6100202749       |AAA2778202       |
|201163         |10740000822407     |10740000822606     |2012         |6100202749       |AAA2778202       |
|201232         |18604177741067     |18604177741366     |2012         |6100202749       |AAA2778202       |
|201295         |18604221522337     |18604221523836     |2012         |6100202749       |AAA2778202       |
|201480         |18604113309952     |18604113310131     |2012         |6100202749       |AAA2778202       |
|201781         |18604199150436     |18604199150835     |2012         |6100202749       |AAA2778202       |
|201480         |6001400030046472   |6001400030046771   |2012         |6100202520       |AAA2777953       |
|202853         |6001700000180323   |6001700000180722   |2012         |6100202520       |AAA2777953       |
-----------------------------------------------------------------------------------------------------------