Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 601187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:41:14+00:00 2026-05-13T16:41:14+00:00

Occasionally I use AWK to extract and/or reverse columns in a data file. awk

  • 0

Occasionally I use AWK to extract and/or reverse columns in a data file.

awk '{print $2,",",$1}' filename.txt

How would I do the same using Emacs Lisp?

(defun awk (filename col1 &optional col2 col3 col4 col5)
  "Given a filename and at least once column, print out the column(s)
values in the order in which the columns are specified."
...
)
;; Test awk
(awk "filename.txt" 1); Only column 1
(awk "filename.txt" 2 1); Column 2 followed by column 1
(awk "filename.txt" 3 2 1); Columns 3,2 then 1

Sample filename.txt:

a   b  c
1   2  5

Sample output:

b , a
2 , 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:41:14+00:00Added an answer on May 13, 2026 at 4:41 pm

    How do you intend to use this? Are you planning on using it as a command-line script? In which case, you’ll need to package it like this hello world question.

    Or, are you planning on using it interactively, in which case you probably want the output in a new buffer…

    This code gets the basics done. You’ll need to update it to match your usage model.

    (defun awk (filename &rest cols)
      "Given a filename and at least once column, print out the column(s) values
    in the order in which the columns are specified."
      (let* ((buf (find-file-noselect filename)))
        (with-current-buffer buf
          (while (< (point) (point-max))
            (let ((things (split-string (buffer-substring (line-beginning-position) (line-end-position))))
                  (c cols)
                  comma)
              (while c
                (if comma
                    (print ", "))
                (print (nth (1- (car c)) things))
                (setq comma t)
                (setq c (cdr c)))
              (print "\n")
              (forward-line))))
        (kill-buffer buf)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 314k
  • Answers 314k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Install GlassFish as a service that starts automatically: Check out… May 13, 2026 at 11:05 pm
  • Editorial Team
    Editorial Team added an answer Look up how to use ExternalInterface. You can call any… May 13, 2026 at 11:05 pm
  • Editorial Team
    Editorial Team added an answer Check out regular-expressions.info This will do what you want: ([^"]+) May 13, 2026 at 11:05 pm

Related Questions

Occasionally I need to use fixed-width integers for communication with external devices like PLCs.
I use bash, and have done so for over a decade - but occasionally
For a given MySQL DB that I use and modify occasionally I had to
Occasionally I'll write a PHP function with a single input, an associative array containing
I've started to add the time taken to render a page to the footer

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.