05-D.7.3: Handling Text Files - cut/paste
- Page ID
- 32344
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)The cut Command
The cut command cuts out the sections from each line of files and writes the result to standard output. It can be used to cut parts of a line by byte position, character and field. Basically the cut command slices a line and extracts the text. Specify an option with the command otherwise it throws an error. If more than one file name is provided then data from each file is not preceded by its file name.
Syntax:
cut [ OPTION ] [FILE]...
Command Options:
Options | Option Meaning |
---|---|
-b, --bytes=,LIST/ | select only these bytes |
-c, --characters=,LIST/ | select only these characters |
-d, --delimiter=,DELIM/ | use DELIM instead of TAB for field delimiter - VALID ONLY FOR FIELDS |
-f, --fields=,LIST/ | select only these fields; also print any line that contains no delimiter character, unless the -s option is specified |
-s, --only-delimited | do not print lines not containing delimiters |
So cut can be used look at a file by the byte, the character or fields. In the example below we have a file statecap.txt - it is three fields that are separated by a colon.
pbmac@pbmac-server $ cat statecap.txt
Montana:Helena:MT
Arizona:Phoenix:AZ
South Dakota:Pierre:SD
Missouri:Jefferson City:MO
Alaska:Juneau:AK
Wyoming:Cheyenne:WY
Nevada:Carson City:NV
We can cut a range of bytes/characters/fields. The first example we cut only the first 3 bytes of the file. In the second we cut the first eight characters. (The bytes and characters are often the same - but bytes will pick up non-characters). In the third example we specify the colon delimiter and the third field - so we get the state abbreviation.
pbmac@pbmac-server $ cut -b 1-3 statecap.txt
Mon
Ari
Sou
Mis
Ala
Wyo
Nev
pbmac@pbmac-server $ cut -c 1-8 statecap.txt
Montana:
Arizona:
South Da
Missouri
Alaska:J
Wyoming:
Nevada:Cpbmac@pbmac-server $ cut -d':' -f3 statecap.txt
MT
AZ
SD
MO
AK
WY
NV
As with most commands we can combine cut with other command input/output to form more powerful command string. We cat the file, cut the first field, then sort it in alphabetic order.
pbmac@pbmac-server $ cat statecap.txt | cut -d':' -f 1 | sort -
Alaska
Arizona
Missouri
Montana
Nevada
South Dakota
Wyoming
The paste Command
The paste command is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or a dash (“-“) is inserted instead of file name, paste reads from standard input and gives output as it is until a interrupt command [Ctrl-c] is given.
Syntax:
paste [ OPTIONS ] [FILES]...
Command Options:
Options | Option Meaning |
-d, --delimiters=, LIST/ |
|
-s, --serial |
|
-z, --zero-terminated | line delimiter is NUL, not newline |
The paste command will take each specified element from each file (in this case the first line of each file) and concatenate them together on a single line. Obviously the files must be in sync with one another otherwise the output would be gibberish.
pbmac@pbmac-server $ paste state.txt capital.txt abbrev.txt
Montana Helena MT
Arizona Phoenix AZ
South Dakota Pierre SD
Missouri Jefferson City MO
Alaska Juneau AK
Wyoming Cheyenne WY
Nevada Carson City N
Adapted from:
"cut command in Linux with examples" by Saloni Gupta, Geeks for Geeks is licensed under CC BY-SA 4.0
"Paste command in Linux with examples" by Akash Gupta, Geeks for Geeks is licensed under CC BY-SA 4.0