简体   繁体   中英

how can i remove the spaces in the file and make the lines variable in a file?

i have two questions.

  1. I have several files separately. I'm trying to pull some lines from them and combine them all in one file. When I put them together, there is a one line space between the lines. When I export to excel, I also have to delete the lines in between. The last line of the file is a space. Is there a method where I can pull only the previous line from the file?

  2. I am using a linux based program. I want to automate the code so as not to copy and paste data one by one. For this, I need to define line-by-line data in a file as a variable for your code.

1-- for the first problem,

I have 100 summary files. I want to combine the last 2 lines of them into one file. For this, when I run the code below, a new file is created as a 1-line space in between because of the last blank line.

awk -F' ' '{print $2,$3,$4,$5,$6,$7}' Summary_${k} | tail -n 2 >> xyz_coordinates

2-- Second problem,

The program converts xyz coordinates to local coordinates. How can I run the coordinates in the xyz_coordinates file I created above, one by one, in the code below? In the program I use, the conversion code works like this,

echo 4208830.039709186 2334850.551667509 4171267.377406844 -6.753E-01 4.493E-01 2.849E-01  | xyz2env.py

Given your description, you want to get the last two lines of each file, not the last two lines of the concatenation of all the files (like you're doing).

If you're using GNU or BSD tail then you can use the -q switch:

tail -q -n 2 Summary_* |
awk '{print $2,$3,$4,$5,$6,$7}' > \
xyz_coordinates

For the blank lines problem, if your input files can contain an arbitrary number of blank lines at the end then you cannot use tail , but here's a solution with awk :

awk '
    FNR == 1 {
        for (nr in tail) {
            print_record(tail[nr])
            delete tail[nr]
        }
    }
    NF >= 7 {
        tail[++nr] = $0
        delete tail[nr-2]
    }
    END {
        for (nr in tail)
            print_record(tail[nr])
    }
    function print_record(s,    a) {
        split(s,a)
        print a[2],a[3],a[4],a[5],a[6],a[7]
    }
' Summary_*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM