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

Related Questions

I'm forced to use JUnit 3. If I were using JUnit 4, I would
I have a Windows application written in C++ that occasionally evaporates. I use the
I occasionally use fuslogvw to track down problems loading assemblies. After all my time
I occasionally use a volatile instance variable in cases where I have two threads
I have coworkers who occasionally use typedef to avoid typing. For example: typedef std::list<Foobar>
Occasionally I use the bash command to replace string in previous command: ^foo^bar Today
Occasionally, I've come across a webpage that tries to pop open a new window
Occasionally while attempting to save a Crystal Report that I'm working on in VS2008,
Occasionally I come accross a unit test that doesn't Assert anything. The particular example
Occasionally, on a ASP (classic) site users will get this error: [DBNETLIB][ConnectionRead (recv()).]General network

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.